Author Topic: Nullpointer exception  (Read 2818 times)

0 Members and 1 Guest are viewing this topic.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Nullpointer exception
« on: January 25, 2013, 03:37:04 pm »
I have the following arrays:
Code: [Select]
JTextArea foxBA, foxMA, foxBP, foxMLS;
JTextArea[] textFox = {foxBA, foxMA, foxBP, foxMLS};
String[] label = {"Breeding Age: ", "Max Age: ", "Breeding Probability: ", "Max Litter Size: "};
I initialize them by doing:
Code: [Select]
for(int i = 0; i < textFox.length; i++){
tab2_fox.add(new JLabel(label[i]));
textFox[i] = new JTextArea(1, 5);
textFox[i].setText(Fox.getData().get(i).toString());
tab2_fox.add(textFox[i]);
}
However, when trying to get the text by doing
Code: [Select]
foxBA.getText() I get a NullPointerException. But when doing
Code: [Select]
textFox[0].getText() everything works fine. Why can't I acces the actual object, but can I acces it by using its array reference?

Some people need a high five in the face... with a chair.
~EC

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Nullpointer exception
« Reply #1 on: January 25, 2013, 03:47:50 pm »
This line:

textFox = new JTextArea(1, 5);

is overwriting foxBA, foxMA, foxBP, and foxML in the array, not actually changing the values of them. If you want to be able to access each element of the array by named variables, perhaps set them equal to their appropriate array elements after the array has been initialized?
« Last Edit: January 25, 2013, 03:49:13 pm by Runer112 »

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Nullpointer exception
« Reply #2 on: January 25, 2013, 03:58:27 pm »
I see. Any suggestions on how to make it change the value?

Some people need a high five in the face... with a chair.
~EC

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Nullpointer exception
« Reply #3 on: January 25, 2013, 04:12:20 pm »
It's not possible to change the values of foxBA, foxMA, foxBP, and foxMLS through the array. Those four variables do not point to elements of the array, nor do the elements of the array point to the variables.


I think the least changes you could perform to make this work would be to change the array initializations to this:
Code: [Select]
JTextArea[] textFox = new JTextArea[4];
String[] label = {"Breeding Age: ", "Max Age: ", "Breeding Probability: ", "Max Litter Size: "};

Then perform your initialization code, and then initialize the individual variables:
Code: [Select]
JTextArea foxBA = textFox[0], foxMA = textFox[1], foxBP = textFox[2], foxMLS = textFox[3];



My object-oriented design teacher would probably yell at any student who used code like this in general, but that's a different matter. :P
« Last Edit: January 25, 2013, 04:16:48 pm by Runer112 »