0

I am trying to do the following

1 - I need to create a class like this

public class base {
    int a;
    String b;
    public base() {
      a = 0;
      b = "";
    }
}

2 - I need to create a class that creates an array of "base" and sets some values

public class arrayBase {
    public base[] ab = new base[2];    
    public arrayBase() {  
       ab[0].a = 1;
       ab[0].b = "test1";
       ab[1].a = 2;
       ab[1].b = "test2";       
    }
}

3 - I need to use "arrayBase" in another class

public class test{
    public static void main(String[] args) {
       arrayBase p = new arrayBase();
       System.out.println(p.ab[0].a);
    }
}

When I try this it gives an error

Exception in thread "main" java.lang.NullPointerException.

How can I solve that problem?

5
  • 1
    You need to create a new base object for each slot of the array. ab[0] = new base(); etc. Commented Jan 29, 2014 at 16:00
  • Your base class would not compile. Commented Jan 29, 2014 at 16:00
  • 1
    Suggestion for best practices: please capitalize your class names (ArrayBase instead of arrayBase). Commented Jan 29, 2014 at 16:02
  • thnks ZouZou it solved my problem. Commented Jan 29, 2014 at 16:03
  • This is not Java... this is not even same zip code as Java :( Commented Jan 29, 2014 at 16:07

3 Answers 3

2

The problem is this line:

 public base[] ab = new base[2]; 

Here you are simply reserving heap space for two base objects, but you still need to create them and assign them to the correct array cell, like this:

  public class arrayBase{
    public base[] ab = new base[2];    
    public arrayBase() {  
       ab[0] = new base();
       ab[0].a = 1;
       ab[0].b = "test1";

       ab[1] = new base();
       ab[1].a = 2;
       ab[1].b = "test2";       
    }
  }

And please, name your class Base instead of base!

Sign up to request clarification or add additional context in comments.

1 Comment

tnks for your warning this solved the problem as ZouZou
1

For a recommendation,

  public class arrayBase{
  public base[] ab = new base[2];    
  public arrayBase() {  
     ab[0] = new base();
     ab[1] = new base();
     ab[0].a = 1;
     ab[0].b = "test1";
     ab[1].a = 2;
     ab[1].b = "test2";       
    }
  }

The class base needs to be intialized, also remember that you can only change the variable a and b if the base class is in the same package because of the default protected modifier.

Comments

1

Modify the arraBase constructor as bellow. Problem there is, there are no object in the ab array, but, you try to access declared object's fields. First, you need to populate the arra with declared objects, then assign the value for the fields of those objects. And, Fix the compilation errors

public arrayBase() {  
     ab[0] = new base();
     ab[1] = new base()
     ab[0].a = 1;
     ab[0].b = "test1";
     ab[1].a = 2;
     ab[1].b = "test2";       
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.