• Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer exception  RSS feed

 
anita sam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
given below is part of my code. when i run this am getting nullpointer exception, ---- j=mc.cal();----- in this stmt. what am doing here is tryin to get a mthd(which returns a value) frm a diff pkg. can anyone help me with it....thanks....

**********
import aa.Myclass;
public class Myutil {

public Myclass mc=null;
int j=0;

public void calc()
{
j=mc.cal();
System.out.println("j is" +j);
}


package aa;
public class Myclass {

int i=0;

public int cal()
{
i++;
return i;
}
}
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't use txt msg notation it makes it difficult for people who do not have good English skills to read.

The problem is that you are not creating an instance of your MyClass class.

You can fix this by replacing

public Myclass mc=null;

with

public Myclass mc= new MyClass()
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anita,

The code will surely give an exception as there is no reference assigned to an object and you are trying to execute the method of some reference.

j=mc.cal();

First of all you need to assign the reference to the object you have created by the name of mc.

Cheers,
Rahul
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!