Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception

    hi all. first post and all that.

    ive done on-and-off amateur programming for a good number of years now, starting with C/C++ and just now moving onto java. that being said, im not awfully skilled yet.

    im working on my final project for the introductory java course im taking presently. right now, im working on creating a virtual keyboard. here's what ive got.

    int i = 0, j = 0;
    String[] keyboardText = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
     
    JButton[][] keyboardButton = {new JButton[10], new JButton[9], new JButton[7]};
     
    for(i = 0; i < 3; i++)
    {
    	for(j = 0; j < keyboardText[i].length(); j++)
    		keyboardButton[i][j].setText(Character.toString(keyboardText[i].charAt(j)));
    }

    in my mind, that creates a qwerty keyboard all ready to be laid out in a few JFrames. however, when it runs, i get a NullPointerException on the working line of the nested for loops ( kbB.setText(blahblah); ).

    reading over this code again, it would seem that i dont initialize any of the JButtons in the 2d array. if thats actually causing the error, i have no idea how to fix it. haha. help?

    if anyone wants me to post the rest of my source code, id be glad to.

    thanks in advance for any replies. =)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception

    You've created arrays but need to instantiate the elements of those arrays. For example:

    for(i = 0; i < 3; i++)
    {
    	for(j = 0; j < keyboardText[i].length(); j++){
    		keyboardButton[i][j] = new JButton();
    		keyboardButton[i][j].setText(Character.toString(keyboardText[i].charAt(j)));
           }
    }

    Or just use a simple 1-liner
    keyboardButton[i][j] = new JButton(Character.toString(keyboardText[i].charAt(j)));

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception

    thanks very much. =) the one liner worked perfectly. =)

Similar Threads

  1. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  2. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  3. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM