user3167555
user3167555

Reputation: 73

Dialog Fragment throws null pointer exception after implementing it into MainActivity

I am trying to use a AlertDialog in my application, but it requires an input to be given. So i was forced to create an interface and override the onAttach() method within my custom DialogFragment class. Once i properly set everything in both the Dialog class and MainActivity, i tried to run my app and it gave me a runtime exception referring to nullpointer among other things. im not sure why this is and i am in need of assistance, please help. Below you will wind the Override of my interface method, the place where my .show() method is called, the code for my DialogFragment class, and my error log lastly.

public class MainActivity extends FragmentActivity implements CDia_exp.NDListener
{

    //Integers and Strings for performing calculations
        int a;
        int b;
        static int deci_cnt;
        int cnt;
        int temp;
        Double exp_x;
        Double exp;
        Double [] num_trk;
        String [] op_trk;
        String num_hold;
        String op_hold;
        String del_hold;

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


    //....Lots of other code here.......

   //Exp_x Button
            opSet[6].setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    if(num_hold != "")
                    {
                        cDia_exp.show(getSupportFragmentManager(), "exp_x");
                        del_hold = tViews[0].getText().toString();
                        for(int x = num_hold.length() - 1; x >= 0; x--)
                        {
                            del_hold = del_hold.substring(0, del_hold.length() - 1);
                        }
                        num_hold = exp.toString();
                        tViews[0].setText(del_hold + num_hold);
                        return;
                    }
                    else
                    {
                    return;
                    }
                }
            });
    }

    //Objects and Overrides for calling foreign functions
        CTrim cTrim = new CTrim();
        CDia cDia = new CDia();
        CDia_exp cDia_exp = new CDia_exp();

        EditText exp_inp = (EditText) findViewById(R.id.exp_inp);

        @Override
        public void onDPClick(DialogFragment dialog)
        {
            exp = Double.parseDouble(num_hold);
            exp_x = Double.parseDouble(exp_inp.getText().toString());
            for(double x = exp_x; x > 0; x--)
            {
                exp *= exp;
            }

        }



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

}

My Custom DialogFragment class

package com.example.musicalc;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;


public class CDia_exp extends DialogFragment

{

    public interface NDListener
    {
        public void onDPClick(DialogFragment dialog);
    } 


    NDListener expListener;


    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);

        try
        {
            expListener = (NDListener) activity;
        }
        catch(ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + "does not implement NDListener");
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {

        AlertDialog.Builder aDia = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        aDia.setView(inflater.inflate(R.layout.calc_dia, null)).setPositiveButton(R.string.diaOk_b, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                expListener.onDPClick(CDia_exp.this);
            }
        }).setNegativeButton(R.string.cancel_b, new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int id) 
            {
                getDialog().cancel();
            }
        });


        return aDia.create();

    }
}

And Now the Error Log

07-20 14:23:43.599: E/Trace(24173): error opening trace file: No such file or directory (2)
07-20 14:23:43.622: D/AndroidRuntime(24173): Shutting down VM
07-20 14:23:43.622: W/dalvikvm(24173): threadid=1: thread exiting with uncaught exception (group=0x4137d2a0)
07-20 14:23:43.653: E/AndroidRuntime(24173): FATAL EXCEPTION: main
07-20 14:23:43.653: E/AndroidRuntime(24173): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicalc/com.example.musicalc.MainActivity}: java.lang.NullPointerException
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.os.Looper.loop(Looper.java:137)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.main(ActivityThread.java:5059)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.reflect.Method.invoke(Method.java:511)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at dalvik.system.NativeStart.main(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173): Caused by: java.lang.NullPointerException
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.Activity.findViewById(Activity.java:1851)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at com.example.musicalc.MainActivity.<init>(MainActivity.java:43)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.Class.newInstanceImpl(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.Class.newInstance(Class.java:1319)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
07-20 14:23:43.653: E/AndroidRuntime(24173):    ... 11 more
07-20 14:25:56.817: E/Trace(24308): error opening trace file: No such file or directory (2)
07-20 14:25:56.934: D/AndroidRuntime(24308): Shutting down VM
07-20 14:25:56.934: W/dalvikvm(24308): threadid=1: thread exiting with uncaught exception (group=0x4137d2a0)
07-20 14:25:57.013: E/AndroidRuntime(24308): FATAL EXCEPTION: main
07-20 14:25:57.013: E/AndroidRuntime(24308): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicalc/com.example.musicalc.MainActivity}: java.lang.NullPointerException
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.os.Looper.loop(Looper.java:137)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.main(ActivityThread.java:5059)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.reflect.Method.invoke(Method.java:511)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at dalvik.system.NativeStart.main(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308): Caused by: java.lang.NullPointerException
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.Activity.findViewById(Activity.java:1851)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at com.example.musicalc.MainActivity.<init>(MainActivity.java:803)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.Class.newInstanceImpl(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.Class.newInstance(Class.java:1319)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
07-20 14:25:57.013: E/AndroidRuntime(24308):    ... 11 more

Upvotes: 1

Views: 399

Answers (1)

user3167555
user3167555

Reputation: 73

Thanks to some experimentation and some research on stackoverflow i have located the answer.

The problem is that the compiler was having trouble locating the view either because it is in a separate xml file from my activity_main.xml file or because i was creating my dialog within a separate class from my MainActiviy.java.

To solve this i created my dialog within my MainActivity.java class outside of the onCreate method. Also i created a layout inflater to inflate my layout within a View in my MainActivity, which was key because the LayoutInflater provided a way to directly call my EditText into code.

The code that fixed the nullpointer when declaring my EditText is as follows:

LayoutInflater inflater = LayoutInflater.from(this);

//Inflater "inflates" layout into View as a value

final View infV = inflater.inflate(R.layout.calc_dia, null);

//Use layout value inflated in View "infV" to find EditText 

final EditText eDTemp = (EditText) infV.findViewById(R.id.expInp);

***The full code for the dialog is as follows****:

public void showCD()
            {
                LayoutInflater inflater = LayoutInflater.from(this);
                final View infV = inflater.inflate(R.layout.calc_dia, null);
                final EditText eDTemp = (EditText)infV.findViewById(R.id.expInp);
                TextView tView0 = (TextView)findViewById(R.id.calcView);
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setView(infV).setPositiveButton("Ok", new     
                DialogInterface.OnClickListener() 
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                }}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        dialog.cancel();
                    }
                });

                AlertDialog alert = builder.create();
                builder.show();
            }

When you are ready for the dialog to appear, simply call the function that contains it.

Upvotes: 0

Related Questions