-1

MainActivity.java

public class MainActivity extends Activity {

static Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button)findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Class2 ji = new Class2();
            ji.dothis();
        }
    });}}

Class2.java

 public class Class2 extends MainActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
public void dothis() {

        Toast.makeText(this,"It Doesn't Worked",Toast.LENGTH_LONG).show();

}
}

My Logcat generates NullPoint Exception every time.

java.lang.NullPointerException: Attempt to invoke virtual method ' android.content.res.Resources android.content.Context.getResources()' on a null object reference

I don't know what's wrong.

I face this problem many times, but I never understood the issue.

JAL
  • 41,701
  • 23
  • 172
  • 300
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Luke Joshua Park Feb 16 '16 at 06:46
  • why you extend from MainActivity ? you can remove the extend and use it as normal class :- 1-Create object from this class 2- using this object call any public or protected function on this class I see that you don't need to extend from MainActivity – Salah Nour ElDin Feb 16 '16 at 07:44

3 Answers3

0

Nothing wrong according to java syntax, you will always be able to instantiate a class by its default constructor. Nothing wrong in how you call Toast.makeText() neither. The only reason it produce null pointer exception is you instantiating the Activity inherited class manually. You should never instantiate and call it manualy, you have to call it via an intent so it will be bound to android activity and therefore the context is initialized. The null pointer error you found it from first parameter of Toast.makeText() which is context.

Ferico Samuel
  • 195
  • 3
  • 15
0

You need some code like this

MainActivity.java

Class2 ji = new Class2(MainActivity.this);
ji.dothis();

On Class2.java

public class Class2{
    MainActivity activity;
    public Class2(MainActivity activity)
    {
        this.activity=activity;
    }
    public void dothis() {
        activity.Toast.makeText(activity,"It Doesn't Worked",Toast.LENGTH_LONG).show();
    }
}

this is worked and tested answer ...

madcookie
  • 16
  • 2
0

please update your Class Class2 as the following :-

public class Class2 {
    Context CurrentContex=null;
    public Class2(Context con)
    {
        CurrentContex=con;
    }
    public void dothis() {
        Toast.makeText(CurrentContex, "It Doesn't Worked", Toast.LENGTH_LONG).show();
    }
}

and update the calling to be like the following :-

 Class2 cs=new Class2(MainActivity.this);
                cs.dothis();
Salah Nour ElDin
  • 510
  • 2
  • 13