-1

When I run my application, I get a NullPointerException, so I can't figure out what's going on here.

My MainActivity.java:

package com.example.ahmad.tapleviewmy;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.ahmad.tapleviewmy.DB.Adapter;
import com.example.ahmad.tapleviewmy.myItem.Item;
import de.codecrafters.tableview.TableView;
import de.codecrafters.tableview.listeners.TableDataClickListener;
import de.codecrafters.tableview.toolkit.SimpleTableDataAdapter;
import de.codecrafters.tableview.toolkit.SimpleTableHeaderAdapter;

public class MainActivity extends Activity{
private EditText nameEditText,propeliantEditText,destEditText;
private Button SaveBtn,ShowDialog;
private TableView <String[]> tb;
private TableHelper tapleHelper;

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

    //TABLE VIEW
    tapleHelper=new TableHelper(this);
    tb=(TableView<String[]>)findViewById(R.id.tableview);
    tb.setColumnCount(3);
    tb.setHeaderBackgroundColor(Color.parseColor("#2ecc71"));
    tb.setHeaderAdapter(new SimpleTableHeaderAdapter(this,tapleHelper.getItemProbeHeaders()));
    tb.setDataAdapter(new SimpleTableDataAdapter(this,tapleHelper.getItemProbe()));

    ShowDialog = (Button) findViewById(R.id.ShowDialogBtn);
    ShowDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayDialog();
        }
    });

    //TABLE CLICK
    tb.addDataClickListener(new TableDataClickListener<String[]>() {
        @Override
        public void onDataClicked(int rowIndex, String[] clickedData) {
            Toast.makeText(MainActivity.this,((String[])clickedData)[1],Toast.LENGTH_SHORT).show();
        }
    });


}


//DISPLAY INPUT DIALOG
private void displayDialog(){
    try {
        Dialog d = new Dialog(this);
        d.setTitle("SQLITE DATA");
        d.setContentView(R.layout.dialog_layout);
        SaveBtn = (Button) findViewById(R.id.saveBtn);
        SaveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //INITIALIZE VIEWS
                nameEditText = (EditText) findViewById(R.id.nameEditText);
                propeliantEditText = (EditText) findViewById(R.id.propEditText);
                destEditText = (EditText) findViewById(R.id.destEditText);

                Item i = new Item();
                i.setName(nameEditText.getText().toString());
                i.setPropeliant(propeliantEditText.getText().toString());
                i.setDestination(destEditText.getText().toString());


                if (new Adapter(MainActivity.this).saveItem(i)) {
                    nameEditText.setText("");
                    propeliantEditText.setText("");
                    destEditText.setText("");
                    tb.setDataAdapter(new SimpleTableDataAdapter(MainActivity.this, tapleHelper.getItemProbe()));
                } else {
                    Toast.makeText(MainActivity.this, "Not Saved", Toast.LENGTH_SHORT).show();
                }
            }

        });
        //SHOW DIALOG
        d.show();
    }
    catch (NullPointerException e){
        e.printStackTrace();
    }
}
}

This is my debug log when attempting to open the dialog:

java.lang.NullPointerException
W/System.err:at   com.example.ahmad.tapleviewmy.MainActivity.displayDialog(MainActivity.java:67)
W/System.err:     at     com.example.ahmad.tapleviewmy.MainActivity.access$000(MainActivity.java:19)
W/System.err:     at     com.example.ahmad.tapleviewmy.MainActivity$1.onClick(MainActivity.java:44)
W/System.err:     at android.view.View.performClick(View.java:4438)
W/System.err:     at android.view.View$PerformClick.run(View.java:18422)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:136)
W/System.err:     at a     android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err:     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err:     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err:     at dalvik.system.NativeStart.main(Native Method)
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – David Rawson Jan 02 '17 at 02:32

1 Answers1

0

It would seem that it can't find your button, when calling ShowDialog = (Button) findViewById(R.id.ShowDialogBtn) maybe double check the id's match.

If you look at the docs for findViewById, it says it returns null if it can't find the view

As a side note variable names should start with lowercase letter e.g. firstName, orderNumber etc. See: http://www.javatpoint.com/java-naming-conventions

Isaac
  • 1,442
  • 17
  • 26