-2

This is the log when I run the app on the emulator, I'm new to app development can someone please help? Ill post the code if needed but its divided into multiple activities.

10-17 08:45:43.269    2696-2696/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-17 08:45:43.269    2696-2696/? I/art﹕ Late-enabling JIT
10-17 08:45:43.273    2696-2696/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-17 08:45:43.304    2696-2696/? W/System﹕ ClassLoader referenced unknown path: /data/app/com.victorioussecrets.nwureferencegenerator-2/lib/x86
10-17 08:45:43.427    2696-2696/com.victorioussecrets.nwureferencegenerator D/AndroidRuntime﹕ Shutting down VM
10-17 08:45:43.427    2696-2696/com.victorioussecrets.nwureferencegenerator E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.victorioussecrets.nwureferencegenerator, PID: 2696
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.victorioussecrets.nwureferencegenerator/com.victorioussecrets.nwureferencegenerator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.victorioussecrets.nwureferencegenerator.MainActivity.onCreate(MainActivity.java:31)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-17 08:45:47.140    2696-2696/? I/Process﹕ Sending signal. PID: 2696 SIG: 9

I've fixed the null error and this is now the new log afterwards.

10-17 09:28:23.871    3195-3195/com.victorioussecrets.nwureferencegenerator E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.victorioussecrets.nwureferencegenerator, PID: 3195
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.Spinner.getSelectedItem()' on a null object reference
            at com.victorioussecrets.nwureferencegenerator.MainActivity.onItemSelected(MainActivity.java:70)
            at android.widget.AdapterView.fireOnSelected(AdapterView.java:922)
            at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:911)
            at android.widget.AdapterView.-wrap1(AdapterView.java)
            at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:881)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Here is my MainActivity.java

package com.victorioussecrets.nwureferencegenerator;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


    Spinner spinnerStyles;
    Button btnExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnExit = (Button)findViewById(R.id.btnExit);

        Spinner spinner = (Spinner)findViewById(R.id.spinnerStyles);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.arrStyles,android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


        String spinVal = "";

        spinVal = spinnerStyles.getSelectedItem().toString();

        if(spinVal.equals("NWU Harvard")) {
            startActivity(new Intent(MainActivity.this, nwu_harvard.class));
        }
        if(spinVal.equals("LAW")) {
            startActivity(new Intent(MainActivity.this, law.class));
        }
        if(spinVal.equals("APA")) {
            startActivity(new Intent(MainActivity.this, apa.class));
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

My last error when i run the app and get to nwu_harvard_book.java activity i don't get a crash or error, but when i try the nwu_harvard_article.java activity i get this error.

10-17 10:35:42.499  28256-28256/com.victorioussecrets.nwureferencegenerator E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.victorioussecrets.nwureferencegenerator, PID: 28256
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
            at com.victorioussecrets.nwureferencegenerator.nwu_harvard_article$2.onClick(nwu_harvard_article.java:43)
            at android.view.View.performClick(View.java:5156)
            at android.view.View$PerformClick.run(View.java:20755)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

This is the first nwu_harvard_book activity package com.victorioussecrets.nwureferencegenerator;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class nwu_harvard_book extends AppCompatActivity {

    Button btnBack,btnGenerate;
    String stringf= "",value1= "",value3= "",value2= "",value4= "",value5= "";
    EditText inputTxt = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nwu_harvard_book);

        btnGenerate = (Button) findViewById(R.id.btngenerate);
        btnBack = (Button) findViewById(R.id.btnBack);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnGenerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                inputTxt = (EditText) findViewById(R.id.edtAuthor);
                value1 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtTitle);
                value2 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtDate);
                value3 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtEdition);
                value4 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtTown);
                value5 = inputTxt.getText().toString();

                stringf = (value1+". "+value3+". "+value2+". "+value4+". "+value5+".");


                Intent intent = new Intent(nwu_harvard_book.this, output.class);
                intent.putExtra("output", stringf);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nwu_harvard_book, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is the second nwu_harvard_article activity

package com.victorioussecrets.nwureferencegenerator;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class nwu_harvard_article extends AppCompatActivity {

    Button btnBack,btnGenerate;
    String stringf= "",value1= "",value3= "",value2= "",value4= "",value5= "",value6= "";
    EditText inputTxt = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nwu_harvard_book);

        btnGenerate = (Button) findViewById(R.id.btngenerate);
        btnBack = (Button) findViewById(R.id.btnBack);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnGenerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                inputTxt = (EditText) findViewById(R.id.edtAuthor);
                value1 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtTitle);
                value2 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtDate);
                value3 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtpages);
                value4 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtmagtitle);
                value5 = inputTxt.getText().toString();
                inputTxt = (EditText) findViewById(R.id.edtVolume);
                value6 = inputTxt.getText().toString();


                stringf = (value1+". "+value3+". "+value2+" "+value5+". "+value6+".");


                Intent intent = new Intent(nwu_harvard_article.this, output.class);
                intent.putExtra("output", stringf);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nwu_harvard_book, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

3 Answers3

2

Your Button is Null. You Must initialize it

Button button = (Button)findViewById(R.id.your_id);

or

Button button = new Button(yourContext);

parent.addView(button);
Mohammad Hossein Gerami
  • 1,360
  • 1
  • 10
  • 26
0

There is a problem on your onCreate method of activity . you check your xml button id is same as onCreate.

Button button = (Button)findViewById(R.id.button1);
Binesh Kumar
  • 2,763
  • 1
  • 16
  • 23
0

In All 3 scenario you got NPE(NullPointerException).
First Case - NPE with Button View not initialized within MainActivity: I think line was btnExit.setOnClickListener(.This you solved using solution provide by Hossein Gerami and probably added btnExit = (Button)findViewById(R.id.btnExit);

Second Case - NPE with Spinner not initialized in MainActivity onItemSelected method: Here line was spinVal = spinnerStyles.getSelectedItem().toString(); You fixed it after initializing spinnerStyles or using some another alternative.

Third Case - NPE with EditText is null in nwu_harvard_article: In this case as the line pointed out by Logcat report

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.victorioussecrets.nwureferencegenerator.nwu_harvard_article$2.onClick(nwu_harvard_article.java:43)

In this case line is value4 = inputTxt.getText().toString(); now why here inputTxt is null bcoz above line makes inputTxt null which is inputTxt = (EditText) findViewById(R.id.edtpages); This means layout has no EditText with id as R.id.edtpages.
In both activity nwu_harvard_article and nwu_harvard_book you are assigning same layout i.e setContentView(R.layout.nwu_harvard_book); So please the id's of all EditText views. I am sure you have EditText with ids edtAuthor, edtTitle and, edtDate. That why you do not get NPE till value3 in nwu_harvard_article after that there is issue. So please check layout properly

SUGGESTION NOTE: Please understand exactly what is NPE from here.
And you could have fixed it easily because in the logcat report you can clearly the line which is giving this NPE and which leads to crashing of application.
For example :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
            at com.victorioussecrets.nwureferencegenerator.nwu_harvard_article$2.onClick(nwu_harvard_article.java:43)

In the above line you can seen nwu_harvard_article.java:43. This is hyperlinked line that means you can click on, if you click on this it will take you directly to line within file which is throwing error in your case it would have been value4...Also first line specify why you are getting NPE.

Community
  • 1
  • 1
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26