Results 1 to 4 of 4
  1. #1
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    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.

    Java Code:
    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
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You have to do something like this:
    Java Code:
    		keyboardButton[i][j] = new JButton(Character.toString(keyboardText[i].charAt(j)));
    The way you are doing it, you are trying to call setText() on an object that doesn't exist.

    -Gary-

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    In C/C++ speak, what you did was create an array of NULL pointers. When you try to access the objects in the array, you're getting NullPointerExceptions. What you should do is this:

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

  4. #4
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks very much guys! the replies were in before i got back with my pizza. haha.

    i fixed it with the one-liner suggested by gcalvin. and iluxa, i appreciate youre clarifications / translation. ;)

    im sure i'll have a thread to post in a moment again as i continue working on this project. haha.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  3. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •