Android Sqlite空指针异常---java.lang.NullPointerException [重复]
Posted
技术标签:
【中文标题】Android Sqlite空指针异常---java.lang.NullPointerException [重复]【英文标题】:Android Sqlite Nullpoint Exception --- java.lang.NullPointerExceptio [duplicate] 【发布时间】:2018-10-06 10:14:21 【问题描述】:我是 Java 和 android 的新手当我尝试将我的 android 应用程序连接到 SQLite 时,它会生成一个空点错误,每次它在调用数据库帮助程序类的 onCreate 方法时崩溃应用程序。
我无法在我的代码中找到错误,所以请有人帮助我。
MainActivity.java 代码:
package com.example.sdreddy.dbfinal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
DbNeed dbn;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dd=(Button)findViewById(R.id.button);
dd.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
TextView tv = (TextView) findViewById(R.id.textView);
try
dbn.Addd("SDREDDY");
String zz = dbn.getdata();
tv.setText(zz);
catch (Exception ee)
tv.setText((CharSequence) ee.toString());
);
DbNeed.java 代码
package com.example.sdreddy.dbfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
/**
* Created by SDREDDY on 26-04-2018.
*/
public class DbNeed extends SQLiteOpenHelper
public static final String dbname="Database.db";
public static final String tblname="MyTable";
public DbNeed(Context context)
super(context, dbname, null, 1);
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
sqLiteDatabase.execSQL("create table if not exists "+tblname+" (id text,value text)");
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
sqLiteDatabase.execSQL("drop table if exists "+tblname);
onCreate(sqLiteDatabase);
public void Addd(String Name)
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Random r=new Random();
String idz=String.valueOf(r.nextInt());
cv.put("id",idz);
cv.put("name",Name);
sqLiteDatabase.insert(tblname,null,cv);
sqLiteDatabase.close();
public String getdata()
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
String qu="SELECT * FROM "+tblname;
Cursor crz=sqLiteDatabase.rawQuery(qu,null);
sqLiteDatabase.close();
String sim=crz.getString(0);
return sim;
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.example.sdreddy.dbfinal.MainActivity">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_
android:layout_
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:id="@+id/button"
android:layout_
android:layout_
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_
android:layout_
android:text="TextView"
android:textSize="30sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Log cat of my Code
【问题讨论】:
请添加logcat 【参考方案1】:在setContentView(R.layout.activity_main);
行之后添加以下内容:-
dbn = new DbNeed(this);
这将实例化(设置)dbn
,它在此阶段仅被声明,因此为空。
【讨论】:
以上是关于Android Sqlite空指针异常---java.lang.NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章