Results 1 to 4 of 4
Thread: Null Pointer Exception
- 05-12-2010, 12:25 AM #1
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
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.
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); ).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))); }
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. =)
- 05-12-2010, 12:38 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You have to do something like this:
The way you are doing it, you are trying to call setText() on an object that doesn't exist.Java Code:keyboardButton[i][j] = new JButton(Character.toString(keyboardText[i].charAt(j)));
-Gary-
- 05-12-2010, 12:48 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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))); }
- 05-12-2010, 03:52 AM #4
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 04:17 PM -
Null pointer exception?
By coffee in forum New To JavaReplies: 4Last Post: 08-03-2009, 03:22 AM -
Help with null pointer exception
By gammaman in forum New To JavaReplies: 4Last Post: 07-14-2009, 12:23 AM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks