当前位置: 首页 > 面试题库 >

错误:对空对象的引用

柯建业
2023-03-14
问题内容

我不知道我的代码有什么问题

这是我的片段课

package com.example.gandi.symanlub;


/**
 * A simple {@link Fragment} subclass.
 */
public class Reminder extends Fragment {

    Button btnubah, btnkeluar;
    SessionManager session;

    View rootview;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootview = inflater.inflate(R.layout.fragment_reminder, container, false);

        btnkeluar = (Button)rootview.findViewById(R.id.btnlogout);
        btnkeluar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                session.logoutUser();
            }
        });

        return rootview;

    }

}

这是我的SessionManager.java

package com.example.gandi.symanlub;

@SuppressLint("CommitPrefEdits")
public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    SharedPreferences.Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // nama sharepreference
    private static final String PREF_NAME = "Sesi";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASS = "pass";



    // Constructor
    public SessionManager(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    /**
     * Create login session
     * */
    public void createLoginSession(String email, String pass){
        // Storing login value as TRUE
        editor.putBoolean(IS_LOGIN, true);

        editor.putString(KEY_EMAIL, email);
        editor.putString(KEY_PASS, pass);
        editor.commit();
    }

    /**
     * Check login method wil check user login status
     * If false it will redirect user to login page
     * Else won't do anything
     * */
    public void checkLogin(){
        // Check login status
        if(!this.isLoggedIn()){
            Intent i = new Intent(_context, Login.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            _context.startActivity(i);
            //((Activity)_context).finish();
        }

    }

    /**
     * Get stored session data
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String, String> user = new HashMap<String, String>();

        user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
        user.put(KEY_PASS, pref.getString(KEY_PASS, null));

        return user;
    }

    /**
     * Clear session details
     * */
    public void logoutUser(){
        editor.clear();
        editor.commit();
    }

    public void hapussesi(){
        editor.clear();
        editor.commit();
    }

    public boolean isLoggedIn(){
        return pref.getBoolean(IS_LOGIN, false);
    }
}

运行项目时出现的错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void
com.example.gandi.symanlub.SessionManager.logoutUser()”


问题答案:

您收到错误消息是因为在此行您正在调用:

session.logoutUser();

会话为空,因为它没有在任何地方初始化。您需要在使用前添加一行以对其进行初始化(可以在onCreateView,或onAttach您认为合适的任何地方进行该操作):

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    session = new SessionManager(getActivity());

getActivity用于将a传递Context给构造函数,因为我看到它采用了该参数。



 类似资料:
  • 因此,我试图从firebase向recyclerview显示图像,但它抛出了一个错误。错误提示E/nite competitive:[qarth _ debug:]get patch store::createDisableExceptionQarthFile方法失败。E/AndroidRuntime:致命异常:主进程:com . example . fortnite competitive,PI

  • 我正试图使用cardview显示RecycerView的联系人,但我得到了这个错误:

  • 错误: 我试图通过这个留档https://www.npmjs.com/package/multer来处理MulterError,但遇到了一个问题。 控制台.log(req.body)给出 { },而表单 enctype 等于“多部分/表单数据”。如果我将其更改为“应用程序/ x-www-表单-网址编码”,则req.body是正常的,但文件不会上传到服务器。 穆特用法 从保修页面使用表单enctyp

  • 问题内容: 我正在尝试将自定义对象从活动传递到片段。我选择实现可序列化并使用以下对象传递对象: 我遇到了错误 尝试在空对象引用上调用虚拟方法 当我尝试将我的对象设置为与活动传递的参数相等时。 这是我的对象 接下来是我要传递的活动,我最终希望传递所有四个对象,但是现在我想至少获得一个成功传递的对象。 然后是引发Null Pointer Exception的片段,我已经用“->”包com.cs246.

  • 问题内容: 我正在尝试将自定义对象从活动传递到片段。我选择实现可序列化并使用以下对象传递对象: 我遇到了错误 尝试在空对象引用上调用虚拟方法 当我尝试将我的对象设置为与活动传递的参数相等时。 这是我的对象 接下来是我要传递的活动,我最终希望传递所有四个对象,但是现在我想至少获得一个成功传递的对象。 然后是引发Null Pointer Exception的片段,我已经用“->”包com.cs246.

  • 我有一个用导航抽屉实现viewpager的代码模块,然而,当我运行代码时,我得到以下错误 指向此行 我的舱单 Android:theme=“@style/theme.TabWithPager” 每当发生这样的错误时 1.检查正在使用哪种活动,是简单的Android.app活动还是AppCompatActivity或ActionBarActivity等等。 2.检查扩展的活动类型是否属于compat