null pointer exception

I

Ivan Sofa n.1

hello,

this is my question: why the compiler says:

"exception in thread "main" java.lang.NullPointerException
at Cruciverba.<init>(Cruciverba.java:12) //la riga corriposponde
at MainClass.main(MainClass.java:8)

this is the Class Cruciverba

public class Cruciverba
{
public String parole[];
// costruttore
Cruciverba()
{

parole[1]=new String ("Pallone"); parole[2]=new String ("Ciao");

}


and this is MainClass

public class MainClass
{
public static void main(String[] args)
{
Cruciverba Primo=new Cruciverba();
}
}


it's a run-time error

help me please!!!!

ivan
 
A

ak

you must init parole[] first;
String parole[] = new String[2];

I think that it is a bad idea to make String parole[] public.
 
P

Peter J. Grey

Hi!

Ivan Sofa n.1 said:
parole[1]=new String ("Pallone"); parole[2]=new String ("Ciao");

Before you can assign parole[1], you have to assign parole (e.g. parole
= new String[2]). Is this done anywhere before the assignment?

Peter
 
E

Eric Sosman

ak wrote: [top-posting fixed]
Ivan Sofa n.1 said:
public class Cruciverba
{
public String parole[];
// costruttore
Cruciverba()
{

parole[1]=new String ("Pallone"); parole[2]=new String ("Ciao");

}
[...]

you must init parole[] first;
String parole[] = new String[2];

Change "2" to "at least 3." Java array indices are
zero-based.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top