错误:空指针——数据没有初始化

--------- beginning of crash
2022-10-13 17:17:26.562 26617-26617/com.example.album E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.album, PID: 26617
    java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
        at com.example.album.ui.Album.FragmentAlbum.onViewCreated(FragmentAlbum.java:32)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:900)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8668)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

错误原因:

集合没有初始化,就给她赋值,出现空指针

        private List<AlbumEntity>mData;



        mData.add(new AlbumEntity("https://img1.baidu.com/it/u=3688400740,2343300667&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313",32,55,true));
        mData.add(new AlbumEntity("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fphoto%2F2010-4-2%2Fenterdesk.com-B7215711B63F17326D3C6EDE1D303BDB.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668244460&t=030b9ee00730ed463386f015fa669aeb",152,55,false));
        mData.add(new AlbumEntity("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2Fa%2F53c0dd6eab513.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668244461&t=2bf405aefb4881a74c1988ff3810c89e",52,65,false));
        mData.add(new AlbumEntity("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F6%2F5822eab200abc.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668244461&t=766899a6e815d7539f397a79bcd10353",153,11,false));
        mData.add(new AlbumEntity("https://img0.baidu.com/it/u=191672387,4111662998&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800",24,55,false));

 解决办法:

集合数据要先初始化

private List<AlbumEntity>mData = new ArrayList<>();
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值