简体   繁体   中英

Android NullPointerException When retriving Spinner text

I'm trying to get the selected item of a spinner and convert it into a string then save it as a .txt file then send, everything works fine until I implemented the spinner and now I'm getting a null pointer exception when I selected No and nothing happens when I select Yes. The Lines referenced in the Logcat are the methods being excecuted in Updater() , and the clickedUpdate is the the Updater() method being run.I'm 100% sure the error occurs in the Updater() Method.

The Updater() Method

public String Updater()
{
    Vibrator vibrate = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    TextView dLong = (TextView) findViewById(R.id.textLong);
    TextView dLat = (TextView) findViewById(R.id.textLat);
    EditText xxxxxx = (EditText)findViewById(R.id.POinput);
    EditText xxxxxx = (EditText)findViewById(R.id.splitPOinput);
    Spinner xxxxxchoice = (Spinner) findViewById(R.id.optionselecti);
    String xxxxxx = xxxxxx.getText().toString();
    String dataLat = dLat.getText().toString();
    String dataLong = dLong.getText().toString();
    String xxxxxx = xxxxxx.getText().toString();
    String Update = null;
    TelephonyManager telephonemanager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    String PhoneNumber = telephonemanager.getLine1Number();
    String xxxxxxChoicei = xxxxxxchoice.getSelectedItem().toString();
    int choice = 0;
    boolean Choice;
    if(xxxxxxChoicei.equals("Yes"))
    {
        choice = 1;//virtual number used to indicate spinner choice on data server
        Choice = true; //for if statment below
        vibrate.vibrate(100);
    }
    else if (xxxxxxChoicei.equals("No"))
    {
        choice = 0;
        Choice = false;
    }
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Temperature");
    alert.setMessage("Input Temperature");
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
    alert.setView(input);

    alert.setPositiveButton("Check-In", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            temperature = input.getText().toString();
        }
    });
    if(Choice = false)
    {
        alert.show();
    }
    if(xxxxxxxxxChoicei.equals("No"))
    {
        temperature = "DRY";
    }
    String DataIn = PhoneNumber + "," + dataLong + "," + dataLat+","+dataPO+","+splitpo+","+temperature+","+choice;
    try {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
                File(getFilesDir() + File.separator + "xxxxxxx_LOCATION_DATA.txt")));
        bufferedWriter.write(DataIn);
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    DATAOUT();
    while((!(temperature.equals(""))&& temperature!=null) || xxxxxxxChoicei.equals("No"))
    {
     sendMail("xxxxxxxxxxxxxxx@gmail.com", "DataTest", DATAOUT());
      Update = "Updated";
      deleteFile("xxxxxxx_LOCATION_DATA.txt");
    }

logcat

Caused by: java.lang.NullPointerException
            at javax.mail.internet.MimeUtility.checkAscii(MimeUtility.java:1346)
            at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1069)
            at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1493)
            at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1477)
            at com.example.adrian.trucktracker.Locator.createMessage(Locator.java:134)
            at com.example.adrian.trucktracker.Locator.sendMail(Locator.java:170)
            at com.example.adrian.trucktracker.Locator.Updater(Locator.java:256)
            at com.example.adrian.trucktracker.Locator.clickedUpdate(Locator.java:264)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3860)
            at android.view.View.performClick(View.java:4480)
            at android.view.View$PerformClick.run(View.java:18686)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

So after fixing it so that DATAOUT() no longer returned null (by setting the default string value to String Dataout = ""; ) I got a bunch of errors caused by this statement:

while((!(temperature.equals(""))&& temperature!=null) || xxxxxxxChoicei.equals("No")) { sendMail("xxxxxxxxxxxxxxx@gmail.com", "DataTest", DATAOUT()); Update = "Updated"; deleteFile("xxxxxxx_LOCATION_DATA.txt"); }

which caused an infinite loop, so i fixed that by no longer using the while, however does someone know how to include the while loop without causing my application to crash?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM