Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:29:07+00:00 2026-05-20T06:29:07+00:00

I am getting this error when i use a string array java.lang.NullPointerException The code

  • 0

I am getting this error when i use a string array
java.lang.NullPointerException

The code i used is as follows..

 String[] list = null;  
 String sql = "SELECT * FROM pos_products";  
 ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);  
 while (rs.next()) {  
     System.out.println(rs.getString("product_name"));  
     list[i] = rs.getString("product_name");  
     i++;  
 }

I have to add one more thing after seeign this replies.. First thank you guys for your response.. Now the problem is i am using JList and i have to add content to it.. If i use list i cant add directly. i can user either vector or a string array. what is your thoughts on that ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:29:07+00:00Added an answer on May 20, 2026 at 6:29 am

    Your list is null, thus null[i] throws NullPointerException

    You should consider either initialize it as ChrisJ describes or use a List

    List<String> list = new ArrayList<String>();
    .... 
        list.add( rs.getString("product_name"));
    ....
    

    edit

    This solves two parts, your NullPointerException and the dynamic fetch of the data.

    You mention you need to put this in a JList, in that case, you can still use this approach, but separate the data retrieval from the displaying code.

    So, create a method list this:

     public String[] fetchOptions() { 
          List<String> list = new ArrayList<String>();
          ... db code here
          while .... etc. et 
              list.add( rs.getString("product_name"));
          ... 
          // convert it to String[] 
    
         return list.toArray( new String[list.size()] );
     }
    

    And then call this method where you create your JList

    String [] options = fetchOptions();
    JList aList = ... use it normally 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this error when I use MarshallingView to marshal my list (java.util.List) of
I am getting this error when I use the Linq expression of var emp
i try to use TBXML and getting this error TBXML class method +tbXMLWithURL not
I am trying to use a function pointer and I am getting this error:
Im trying to use Date Formatters (NSDateFormatter), but I keep getting this error: Program
I'm having success when I use this code to get a string from an
I'm trying to parse this string into java, but I keep getting errors. {id:1,jsonrpc:2.0,result:{limits:{end:3,start:0,total:3},sources:[{file:/media/storage/media/re
I'm constantly getting this error whenever I try to pass a array of Strucs
Can someone please tell me where is the error in this code?, I use
I keep getting this error but this sql statement works fine in mysql it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.