简体   繁体   中英

EditText.GetText, return exception about null reference, but it is declared

I'm working on an Android Application and I have some issues.

I have some EditText Inputs and I want to create an Object called Contest with the text from input. My code is the following:

package com.example.torge.projectnow.loginSystem;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.torge.projectnow.R;

public class AddContest extends AppCompatActivity implements View.OnClickListener{

    Spinner sCategoryC = null;
    String idUserUpload = "1";
    EditText etContestName, etShorDesC, etDeadlineC, etAgeLimitC, etStartDateC, etLocatoinC, etFullDesC, etCostC;
    UserLocalStore userLocalStore;
    Button bSendContest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contest);
        sCategoryC = (Spinner) findViewById(R.id.sCategoryContest);
        etContestName = (EditText) findViewById(R.id.etContestName);
        etShorDesC = (EditText) findViewById(R.id.etShortDesC);
        etDeadlineC = (EditText) findViewById(R.id.etDeadlineC);
        etAgeLimitC = (EditText) findViewById(R.id.etAgeLimitC);
        etStartDateC = (EditText) findViewById(R.id.etStartDateC);
        etFullDesC = (EditText) findViewById(R.id.etFullDescC);
        etCostC = (EditText) findViewById(R.id.etCostC);



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton bSendContest = (FloatingActionButton) findViewById(R.id.bSendContest);
        bSendContest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Contest contest = new Contest(etContestName.getText().toString(),
                        sCategoryC.getSelectedItem().toString(),
                        etShorDesC.getText().toString(),
                        etDeadlineC.getText().toString(),
                        etAgeLimitC.getText().toString(),
                        etStartDateC.getText().toString(),
                        etLocatoinC.getText().toString(),
                        etFullDesC.getText().toString(),
                        idUserUpload,
                        etCostC.getText().toString());

                switch (v.getId()){
                    case R.id.bSendContest :
                        if(etContestName.getText().toString().matches("") ||
                                sCategoryC.getSelectedItem().toString().matches("") ||
                                etShorDesC.getText().toString().matches("") ||
                                etDeadlineC.getText().toString().matches("") ||
                                etAgeLimitC.getText().toString().matches("") ||
                                etStartDateC.getText().toString().matches("") ||
                                etLocatoinC.getText().toString().matches("") ||
                                etFullDesC.getText().toString().matches("") ||
                                idUserUpload.matches("") ||
                                etCostC.getText().toString().matches("")){
                            AlertDialog alertDialog = new AlertDialog.Builder(AddContest.this).create();
                            alertDialog.setTitle("Something went wrong");
                            alertDialog.setMessage("Please fill all the boxes");
                            alertDialog.show();

                        }else {
                            ServerRequest serverRequest = new ServerRequest(AddContest.this);
                            serverRequest.StoreContestInBackground(contest);
                            Snackbar.make(findViewById(R.id.addProjectLayout), "The contest Succesfuly added!", Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                            startActivity(new Intent(AddContest.this, Dashboard.class));
                        }

                        break;
                }
            }
        });
    }


    @Override
    public void onClick(View v) {


    }
}

But i get this error

02-29 20:15:49.950 12260-12260/com.example.torge.projectnow E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.torge.projectnow, PID: 12260
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                                  at com.example.torge.projectnow.loginSystem.AddContest$1.onClick(AddContest.java:47)
                                                                                  at android.view.View.performClick(View.java:4788)
                                                                                  at android.view.View$PerformClick.run(View.java:19965)
                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5431)
                                                                                  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:914)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

And I can't resolve it. Can you help me?

You haven't initialize your etLocatoinC

ie

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

and you are attempting to call getText() method on it.

You forgot to cast the TextView etLocatoinC in the onCreate method, that is declared but null referenced... and since you are calling the the getText in the onClick method, then you get a NPE..

Quickfix:

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

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