null pointer exception

Discussion in 'Java' started by Ivan Sofa n.1, Nov 10, 2003.

  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
     
    Ivan Sofa n.1, Nov 10, 2003
    #1
    1. Advertising

  2. Ivan Sofa n.1

    ak Guest

    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" <> schrieb im Newsbeitrag
    news:4ZSrb.108340$...
    > 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
    >
    >
     
    ak, Nov 10, 2003
    #2
    1. Advertising

  3. 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
     
    Peter J. Grey, Nov 15, 2003
    #3
  4. Ivan Sofa n.1

    Eric Sosman Guest

    ak wrote: [top-posting fixed]
    >
    > "Ivan Sofa n.1" <> 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.

    --
     
    Eric Sosman, Nov 17, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,504
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,672
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,587
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    780
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    465
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page