Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 初始化从其他活动获取的长值_Java_Android_Sqlite_Bundle_Long Integer - Fatal编程技术网

Java 初始化从其他活动获取的长值

Java 初始化从其他活动获取的长值,java,android,sqlite,bundle,long-integer,Java,Android,Sqlite,Bundle,Long Integer,我从其他活动中获得一个长值,并希望将其保存在sqlite中 long fk; fk = bundle.getLong("ab"); // value ab get from other activities k.insertStaffBenefit(name,result,fk); StaffAPI.java public class StaffAPI { private SQLiteDatabase database; private MyDatabaseHe

我从其他活动中获得一个长值,并希望将其保存在sqlite中

long fk;
fk = bundle.getLong("ab"); // value ab get from other activities
k.insertStaffBenefit(name,result,fk);
StaffAPI.java

public class StaffAPI {
        private SQLiteDatabase database;
        private MyDatabaseHelper dbHelper;
        public String[] allColumns={MyDatabaseHelper.ID3, MyDatabaseHelper.Description,MyDatabaseHelper.Amount,MyDatabaseHelper.Twd_id};

        public StaffAPI(Context context)
        {
            dbHelper=new MyDatabaseHelper(context);
        }

        public void open() throws SQLException {
            database = dbHelper.getWritableDatabase();
        }

        public void close() {
            dbHelper.close();
        }
        public long insertStaffBenefit(String description,String amount,long id)
        {
            database=dbHelper.getWritableDatabase();
            ContentValues values=new ContentValues();
            values.put(MyDatabaseHelper.Description,description);
            values.put(MyDatabaseHelper.Amount,amount);
            values.put("Twd_id",id);
            database.insert(MyDatabaseHelper.TABLE_STAFF_BENEFICT,null,values);
            database.close();
            return 0 ;

        }

    }
我知道我必须初始化
a
,因为它可能指向null,但我该如何解决这个问题呢

java.lang.NullPointerException:尝试调用虚拟方法“long” com.example.project.project.API.StaffAPI.insertStaffBenefit(java.lang.String, 空对象引用上的java.lang.String,long)”


我不认为您的错误来自
long
NullPointerException
long
是基本数据类型,它有默认值,您不能将
null
值指定为:
long a=null
。不可能。因此,我建议您在分配包之前检查您的
bundle.getLong(“foreignKey”)

long a;
a= bundle.getLong("foreignKey") != null? bundle.getLong("foreignKey") : 0; 
k.insertToDB(a);
编辑:

在日志猫中,当您试图从
null
对象(代码中的k)调用
insertStaffBenefit
时,会发生错误。尝试检查它。

a是长的,不是长的,所以它的默认值是0,不是空的。然后可以查看getLong(“foreignKey”)方法的源代码

它的默认值是0,返回类型是long,而不是long。 所以你不需要判断a是否为空。
错误不是a为空,而是调用insertStaffBenefit。

有人知道如何解决吗?崩溃发生在com.example.project.project.API.StaffAPI.insertStaffBenefit中,请显示此代码并调用代码。请显示您的insertToDB和insertStaffBenefit方法。已编辑..
name
result
在k.insertStaffBenefit(name,result,fk)中使用FineK;是null,请检查它并在此处显示。我执行了类似以下操作:Toast.makeText(getActivity().getApplicationContext(),fk+“”,Toast.LENGTH_LONG).show()并显示
fk
value在使用
k.insertStaffBenefit(名称、结果、a)之前是否初始化了
k
?logcat告诉我们您试图从
null
对象调用
insertStaffBenefit
。这意味着
k在代码中为null
。谢谢..我错过了编写
k=new com.example.project.project.API.StaffAPI(getActivity())