0

let there is a class named Calc.java inside this class there is a method named Min

  public int Min(int one, int two)
    {
        return one - two ;
    }

Now in the main class that nemed First.java :

package first;
public class First {
    public static void main(String[] args) {
        Calc y [] = new Calc[3];

       System.out.println(y[1].Min(5, 3));
    }
}

when Run :

Exception in thread "main" java.lang.NullPointerException at first.First.main(First.java:9) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

Netbeans shows Hint : Unbalanced read/write with arrays

i don't know what's wrong ! Thanks...

3
  • Always check the Java tutorials - they are invaluable: docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Jul 14, 2013 at 20:14
  • It is not enough to declare an array of a class as new() instance - every member in the array must be created as a new instance Commented Jul 14, 2013 at 20:14
  • before print addd y [1] = new Calc; Commented Jul 14, 2013 at 20:15

5 Answers 5

1

You have to initialize the calc object before using it. It is not enough to initialize the array itself, but you have to initialize the calc objects in the array like this:

calc[1]=new calc();
Sign up to request clarification or add additional context in comments.

Comments

1

In the first line in your main method, when you are doing

Calc y [] = new Calc[3];

the values of the array will be,

y[0]=null, y[1]=null, y[2]=null

Why?

Because like when you create an integer, int[] the array is initialized with default values(i.e zero), similarly when an object array is created, it is initialized with null by default.

Now before doing any operation on an array Item, you have to create the object.

so you should do,

y[1] = new Calc();

before calling the Min method.

I hope this made it clear.

Comments

1

y[1] is null, you have to initializes your array:

public static void main(String[] args) {

   Calc y[] = new Calc[3];
   for(int i=0;i<y.legnth;i++){
       y[i] = new Calc();
   }
   System.out.println(y[1].Min(5, 3));
}

Also in Java methods are written starting letter lowerCase. You may interested in this link Arrays

4 Comments

hmmmm is that Calc y [] = new Calc[3]; mean that he initialize by default? or i have to declare every array variable?
@MohamedAnan i don't understand your question, here you are declaring that y array stores 3 elements of Calc type, but thehy are not initialized , they are null
@MohamedAnan no... y[0] y[1] y[2] starts in 0.. and y[0] = null by default.. but if u make primitive array like int[] y ; then it's initializate in 0.. cause null is for objects.. you may want to read the link i posted
@MohamedAnan you don't follow java code conventions.. to others wil be dificuult to understand your code
0
Calc y [] = new Calc[3];
for (int i = 0; i< 3;i++)
{
    y[i] = new Calc();
}

Comments

0

Why are you creating an array there? Is it really needed? And if at all it is needed, you need to populate that array with Calc object first before invoking any instance method.


But, it seems like, your method should be public static, given that Calc class is just a utility class, and Min method is not at all associated with any instance, rather just with that class. So, modify it so:

public static int min(int one, int two)

And then you can just invoke this method on class name:

Calc.min(5, 3);

No need to have different instances of that class. Also take a look at the API of Math class which has similar utility methods. You will get an idea of what I'm talking about.

Moreover, your min method doesn't seem to do what it should do. For e.g., y[i].Min(5, 3) will currently return 2. Strange isn't it? It is not at all returning the minimum of the passed arguments. You should change the return statement to:

return one < two ? one : two;

If you understand this, you should further extend it to consider equal numbers.

1 Comment

Woow it work...... actually I'm a beginner java programmer and this is is a test my new lesson>>>> thanks

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.