1

I have declared:

Discursiva[] questoesDiscursivas = new Discursiva[10];

Which is:

public class Discursiva extends Questao{
    private String criteriosCorrecao;
}

And Questao is:

public class Questao {
    private String pergunta;
    private double peso;
}

So I just did:

str = JOptionPane.showInputDialog("Pergunta da Questao:");
questoesObjetivas[i].setPergunta(str);

And got java.lang.NullPointerException.

I have read in oracle docs: Thrown when an application attempts to use null in a case where an object is required. These include:

Calling the instance method of a null object. 
Accessing or modifying the field of a null object. 
Taking the length of null as if it were an array. 
Accessing or modifying the slots of null as if it were an array. 
Throwing null as if it were a Throwable value.

And I dont get why I am receiving NullPointerException. Please dont bottomrate my question, Iam still learning Java, its like my first code in it and I would like your help, so please, how can I fix this?

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
  • 1
    Where are you initializing `questoesObjetivas`? – Petar Minchev Sep 03 '12 at 14:17
  • What is `questoesObjetivas`, how large is it and what is the value of `i` when the exception is thrown? – Emil Vikström Sep 03 '12 at 14:18
  • It would help to see all the code you are usign, or at least what's the code inside the setPergunta(String) function – Frank Orellana Sep 03 '12 at 14:19
  • 1
    If you looked at your code in a debugger you would be able to see that `questoesDiscursivas` is full of `null`s. This is because creating an array in Java doesn't implicitly fill the array with objects. – Peter Lawrey Sep 03 '12 at 14:20
  • Both SLaks and AmitD, made me see my error, thanks all of you! – Guilherme Garcia da Rosa Sep 03 '12 at 14:21
  • `Discursiva[] questoesDiscursivas = new Discursiva[10];` only initializes an array of 10 references, each reference pointing to nowhere! So you need to make each of them point to an actual object by something like `questoesObjetivas[i] = new Discursiva();` – Curious Sep 03 '12 at 14:57

7 Answers7

6

Your array is full of nulls.

You need to put a new Discursiva() in each slot of the array before you can use it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

Before doing this questoesObjetivas[i] first do this

 questoesObjetivas[i] = new Discursiva();

You will find a good article over here.

Array only contains only references to objects if you declare Array of objects.Each value inside Array of object is initialized to null. So when you try to access it you will get NullPointerException

Because arrays are covariant [if Sub is a subtype of Super, then the array type Sub[] is a subtype of Super[] **Effective Java**] in nature one should prefer ArrayList

ArrayList<Discursiva> questoesDiscursivas = new ArrayList<Discursiva>(10);
questoesDiscursivas.add(new Discursiva());

You can read more in Effective Java Item 25: Prefer lists to arrays

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
2

You need to create the object in your array:

Discursiva[] questoesDiscursivas = new Discursiva[10];

for (int i=0;i<10;i++){
    questoesDiscursivas = new Discursiva();
}
Resh32
  • 6,500
  • 3
  • 32
  • 40
1

you have declared questoesObjetivas[i], but not initialised it.

Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
0

Upon creation, all of the elements of object arrays are initialized to null, so questoesObjetivas[i].setPergunta(str) will give a NullPointerException since the ith element of that array is indeed null. Try filling the array with instances of the Discursiva class.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

Guessing the problem is in the two lines you gave :

questoesObjetivas[i] is null or questoesObjetivas is null

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
0

you need to instanitate an Object so you can put it inyour Array:

   questoesObjetivas[i] = new setPergunta(str);
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130