Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > null pointer exception

Reply
Thread Tools

null pointer exception

 
 
Ivan Sofa n.1
Guest
Posts: n/a
 
      11-10-2003
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:

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


 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      11-10-2003
you must init parole[] first;
String parole[] = new String[2];

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


"Ivan Sofa n.1" <(E-Mail Removed)> schrieb im Newsbeitrag
news:4ZSrb.108340$(E-Mail Removed)...
> 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:
>
> 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
>
>



 
Reply With Quote
 
 
 
 
Peter J. Grey
Guest
Posts: n/a
 
      11-15-2003
Hi!

"Ivan Sofa n.1" wrote:

>
> 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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-17-2003
ak wrote: [top-posting fixed]
>
> "Ivan Sofa n.1" <(E-Mail Removed)> schrieb
> >
> > 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
NULL macro vs. 0 as null pointer? Ken C++ 69 07-31-2004 04:45 AM
Followup: NULL macro vs. 0 as null pointer? Ken C++ 5 07-24-2004 04:05 PM



Advertisments