EditText使用SetText时出现空指针异常

qq_29954515 2016-07-15 09:59:14
代码如下:
package com.sings.tushu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* Created by Administrator on 2016/7/14.
*/
public class StudentDetail extends ActionBarActivity implements View.OnClickListener{

private Button btnsave,btndelete,btnclose;
private EditText edname,edage,edemail;
private int _student_id=0;
Student student;

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



btnsave= (Button) findViewById(R.id.btnSave);
btnsave.setOnClickListener(this);
btnclose= (Button) findViewById(R.id.btnClose);
btnclose.setOnClickListener(this);
btndelete= (Button) findViewById(R.id.btnDelete);
btndelete.setOnClickListener(this);

edname= (EditText) findViewById(R.id.edName);
edname.setText(student.name);
edage= (EditText) findViewById(R.id.edAge);
edage.setText(String.valueOf(student.age));
edemail= (EditText) findViewById(R.id.edEmail);
edemail.setText(student.email);

_student_id=0;
Intent intent = getIntent();
_student_id = intent.getIntExtra("student_Id",0);
StudentRepo repo = new StudentRepo(this);
student = repo.getStudentById(_student_id);

}

@Override
public void onClick(View v) {
if (v==findViewById(R.id.btnSave)){
StudentRepo repo = new StudentRepo(this);
Student student = new Student();
student.age=Integer.parseInt(edage.getText().toString());
student.email=edemail.getText().toString();
student.name=edname.getText().toString();
student.student_Id=_student_id;

if (_student_id==0){
_student_id=repo.insert(student);
Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
}else{
repo.update(student);
Toast.makeText(this,"Student Record updated",Toast.LENGTH_SHORT).show();
}
}else if (v==findViewById(R.id.btnDelete)){
StudentRepo repo = new StudentRepo(this);
repo.delete(_student_id);
Toast.makeText(this,"Student Record Deleted",Toast.LENGTH_SHORT).show();
finish();
}else if (v==findViewById(R.id.btnClose)){
finish();
}
}
}
红字位置就是出现空指针的地方
搞了一天没弄好,麻烦大神帮忙看看,谢谢!!
是能进入界面的,但一按添加按钮,就出错了,以下是LOG

07-15 21:55:04.398 4395-4395/com.sings.tushu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sings.tushu, PID: 4395
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sings.tushu/com.sings.tushu.StudentDetail}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference
at com.sings.tushu.StudentDetail.onCreate(StudentDetail.java:37)
at android.app.Activity.performCreate(Activity.java:6262)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5885) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
...全文
552 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
china-zhz 2020-02-16
  • 打赏
  • 举报
回复
他这个意思是没有找到student.name先private student. name = null;试试先赋值为空,
a392124835 2019-06-29
  • 打赏
  • 举报
回复
前面几楼说了办法但是你们能写个代码吗?这样解答问题等于没解答吧
風言楓語 2016-08-04
  • 打赏
  • 举报
回复
很明显是Student.空了 textview是可以设置null值的 Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference
山鹰1985 2016-08-03
  • 打赏
  • 举报
回复
引用 楼主 qq_29954515 的回复:
代码如下: 是能进入界面的,但一按添加按钮,就出错了,以下是LOG 07-15 21:55:04.398 4395-4395/com.sings.tushu E/AndroidRuntime: FATAL EXCEPTION: main Process: com.sings.tushu, PID: 4395 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sings.tushu/com.sings.tushu.StudentDetail}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) at android.app.ActivityThread.access$900(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:168) at android.app.ActivityThread.main(ActivityThread.java:5885) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference at com.sings.tushu.StudentDetail.onCreate(StudentDetail.java:37) at android.app.Activity.performCreate(Activity.java:6262) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)  at android.app.ActivityThread.access$900(ActivityThread.java:150)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:168)  at android.app.ActivityThread.main(ActivityThread.java:5885)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.sings.tushu.Student.name' on a null object reference 引发错误的原因:空指针意外: 试图去读取字符串,tushu(图书).Student.name 在一个空的对象引用上. 也就是说:你的 edname= (EditText) findViewById(R.id.edName); 和 XML 里面的对应不上.
Stanny_Bing 2016-07-18
  • 打赏
  • 举报
回复
什么鬼、、、代码也是有先后的好伐,先赋值
qq840727854 2016-07-16
  • 打赏
  • 举报
回复
引用 6 楼 qq_29954515 的回复:
@qq840727854 我的student=repo.getStudentById(_student_id); 赋值了呀
edname.setText(student.name); edage= (EditText) findViewById(R.id.edAge); edage.setText(String.valueOf(student.age)); edemail= (EditText) findViewById(R.id.edEmail); edemail.setText(student.email); _student_id=0; Intent intent = getIntent(); _student_id = intent.getIntExtra("student_Id",0); StudentRepo repo = new StudentRepo(this); student = repo.getStudentById(_student_id); 是新手么??? 前面 用到了 后面才赋值? 能不空么
qq_29954515 2016-07-16
  • 打赏
  • 举报
回复
@qq840727854 我的student=repo.getStudentById(_student_id); 赋值了呀
qq_29954515 2016-07-16
  • 打赏
  • 举报
回复
@liudashao0 什么意思? 是要把student.name=edname.getText().toString();放到onCreate里吗?
qq840727854 2016-07-16
  • 打赏
  • 举报
回复
。。。。。。是我眼睛花了么。。。。。这不是很明显 student 为空么 所以student.name 肯定报错吖 你student压根没赋值吖。。。 上面几位 怎么 还在扯来扯去
ink_s 2016-07-16
  • 打赏
  • 举报
回复
student.name这个是空的,你可以先让他等于null,
liudashao0 2016-07-15
  • 打赏
  • 举报
回复
你在点击事件里处理的给name赋值的逻辑,调用oncreate()的时候,你的对象没有还没有创建呢,EditText.setText()肯定报空指针异常
mis_take 2016-07-15
  • 打赏
  • 举报
回复
是不是在xml文件里的id不对应,EditText对象没有构造出来?

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧