Java 尝试设置用户输入的id时出现NullPointerException

Java 尝试设置用户输入的id时出现NullPointerException,java,android,Java,Android,我试图在我的android项目上工作,但在我的主函数中得到了一个空指针异常。 因此,基于输入id,我希望设置从editText输入的id,并在调用get函数时检索它,该函数将完成条件的where子句。 这是我的密码: /*---GetSet.java--*/ 以下是我的MainActivity.java文件 public class MainActivity extends Activity { EditText etGWid; Button OKbtn; public final stat

我试图在我的android项目上工作,但在我的主函数中得到了一个空指针异常。 因此,基于输入id,我希望设置从editText输入的id,并在调用get函数时检索它,该函数将完成条件的where子句。 这是我的密码:

/*---GetSet.java--*/

以下是我的MainActivity.java文件

public class MainActivity extends Activity {

EditText etGWid;
Button OKbtn;

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    
    etGWid = (EditText)findViewById(R.id.etGWID);
    OKbtn = (Button)findViewById(R.id.OKbtn);
    
    final GetSet obj_getset = null;
    final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);  
            // Create a shared preferences variable using the SharedPreferenceManager for storing GWids
    
    SharedPreferences.Editor editor = app_preferences.edit();  // We also need a shared preferences editor to handle the shared preference requests
    // Call the edit method (library function) to editor variable can edit the key, values.
    
    editor.putInt("47688507", 47688507);   // put in the shared preferences values of user GWids and give them a key for retrieval purposes
    editor.putInt("1234567", 1234567);
    editor.putInt("7654321", 7654321);
    
    editor.commit();   //commit is necessary to save the shared preferences
    
    
    
    OKbtn.setOnClickListener(new View.OnClickListener() {
        
        @SuppressWarnings("null")
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            
            String gwidCheck = etGWid.getText().toString();  //get the value user enters for GWid
            
            if(app_preferences.contains(gwidCheck))       // this will check the stored shared preferences and compare with the value entered
            {
                Context context = getApplicationContext();
                CharSequence text = "Login Successfull";
                int duration = Toast.LENGTH_SHORT;                              //If it exists, then create a toast message for success

                
                
                //etGWid.setText("");    // make the textbox empty
                long setid = Long.parseLong(gwidCheck);   // take the string gwid and convert to long
                obj_getset.setId(setid);    // set the gwid entered
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                intentfunction();
            } 
            else
            {
                Context context = getApplicationContext();
                CharSequence text = "Login Failed";                     // If doesnt exist, create a toast for fail
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            
        }
    });
}


private void intentfunction()
{
     Intent intent = new Intent(this, SelectOptions.class);
     //editText = (EditText) findViewById(R.id.editText1);
     //editText = new EditText(this);
     
    String message = "TestHello";
    intent.putExtra(EXTRA_MESSAGE, message);
    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.activity_main, menu);
    return true;
}

}
第69行是我遇到问题的地方,问题如下:

 obj_getset.setId(setid);
这是MySQLitehelper.java文件中用于根据id检索行的方法之一:

 public Cursor getRecord(long rowId) throws SQLException
{
        Cursor mCursor =
        db.query(true, TABLE_NAME, new String[] {COLUMN_ID,
        COLUMN_DATE, COLUMN_LOCATION, COLUMN_TIME},
        COLUMN_ID + "=" + getset.getId(), null, null, null, null, null);
        if (mCursor != null) 
        {
            mCursor.moveToFirst();
        }
 return mCursor;
 }
我的问题是,如何设置用户输入的值,以便获得值,并根据该值,将条件放入where子句中,如上所述。我哪里做错了

LogCat
程序意外关闭。

final GetSet obj_GetSet=null


当然你会得到一个nullPointerException;您的obj_获取集被定义为空。

您面临什么问题?@Rohit Jain:应用程序关闭unexpectedly@Erolobj_getset.setId(setId)@noobcoder:代码中的第69行是什么?obj_getset定义在哪里?@SuppressWarnings(“null”)也没有帮助。@JesseJ:Duhh。。。!!!我真蠢。我只是没有正确初始化对象。@JesseJ:现在,我有另一个问题。我的getset对象被指定为getset getset=new getset();在方法getRecord中,行#COLUMN_ID+“=”+getset.getId(),null,null,null,null,null);#存在空指针异常。我想我在获取MainActivity.java文件中设置的id时遇到了问题。你们明白我想说什么吗?由于初始化,无论我使用setId方法设置了什么id,我都不知道如何从另一个类中检索它。我不确定。如果你有不同的问题,我建议你提出不同的问题。
 public Cursor getRecord(long rowId) throws SQLException
{
        Cursor mCursor =
        db.query(true, TABLE_NAME, new String[] {COLUMN_ID,
        COLUMN_DATE, COLUMN_LOCATION, COLUMN_TIME},
        COLUMN_ID + "=" + getset.getId(), null, null, null, null, null);
        if (mCursor != null) 
        {
            mCursor.moveToFirst();
        }
 return mCursor;
 }
11-29 21:19:22.525: D/AndroidRuntime(10323): Shutting down VM
11-29 21:19:22.525: W/dalvikvm(10323): threadid=1: thread exiting with uncaught         exception (group=0x40a70930)
11-29 21:19:22.583: E/AndroidRuntime(10323): FATAL EXCEPTION: main
11-29 21:19:22.583: E/AndroidRuntime(10323): java.lang.NullPointerException
11-29 21:19:22.583: E/AndroidRuntime(10323):    at     com.example.upd.MainActivity$1.onClick(MainActivity.java:69)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at     android.view.View.performClick(View.java:4202)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.view.View$PerformClick.run(View.java:17340)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.os.Handler.handleCallback(Handler.java:725)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.os.Looper.loop(Looper.java:137)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.app.ActivityThread.main(ActivityThread.java:5039)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at java.lang.reflect.Method.invoke(Method.java:511)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at dalvik.system.NativeStart.main(Native Method)
11-29 21:19:25.702: I/Process(10323): Sending signal. PID: 10323 SIG: 9