简体   繁体   English

我的应用程序出现Arraylist Nullpointer异常

[英]Arraylist Nullpointer exception with my app

heloo every one i have an application class with a static arraylist, and when i try to add an item to the arraylist i get nullpointer exception.. here is my code heloo每个我都有一个带有静态arraylist的应用程序类,当我尝试将一个项目添加到arraylist时,我会得到nullpointer异常。这是我的代码

public class SomeApp extends Application {
   public static ArrayList<String> ids = new ArrayList<String>();
   //then i have getters and setters for ids..
   public static ArrayList<String> getIds() {
    return ids;
   }
   public static void setIds(ArrayList<String> ids) {
    SomeApp.ids = ids;
   }
}

now this is my service class 现在这是我的服务班

public class BpA extends Service {
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
      // note i created a new thread so the below code could run in..       
      SomeApp.getIds().add(UserIdname);  // i get nullpointer exception on this line
      return START_STICKY;
}

can some1 help me?? 有人可以帮我吗?

SomeAppDid you you set you Application name in manifest file . 您已在清单文件中设置了SomeAppDid。

<application
        android:name="you_package.SomeApp"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >

</application>

Add null checking before add UserIdname in ArrayList. 在ArrayList中添加UserIdname之前,添加空检查。

Check null in your ArrayList if it is null then recreate it. 在ArrayList中检查null是否为null,然后重新创建。

public class BpA extends Service {
 @Override 
 public int onStartCommand(Intent intent, int flags, int startId) {
      // note i created a new thread so the below code could run in..  
if(SomeApp.getIds() != null)      
      SomeApp.getIds().add(UserIdname);  // i get nullpointer exception on this line 
      return START_STICKY;
} 

Try this directly... 直接尝试...

if(UserIdname != null){
    SomeApp.ids.add(UserIdname);
}

im conjuring your code, and running it under all scenarios.. so try this might work first of all remove the getters and setters.. you can access/set ids directly by 我正在召唤您的代码,并在所有情况下运行它。.因此,请尝试首先删除getter和setter的方法。您可以通过以下方式直接访问/设置ID:

SomeApp.ids

and set it by 并设置为

 SomeApp.ids = (another array)

because it is "public static" so no need for getters and setters 因为它是“公共静态”,所以不需要获取器和设置器
2:and also check if the string is empty or not before adding to the array 2:并在添加到数组之前检查字符串是否为空

if(userIdname != null && !userIdname.IsEmpty){
   SomeApp.ids.add(userIdname);
 }

and change the UserIdname to userIdname.. like i did.. possible scenarios for nullpointer exception was from your getter or String..(im guessing) 并将UserIdname更改为userIdname ..像我一样.. nullpointer异常的可能情况是来自您的getter或String ..(我猜是)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的Android应用程式因nullPointer异常而当机 - My android app crash with nullPointer Exception 使用ArrayList时Service onStart中的NullPointer异常 - NullPointer Exception in Service onStart when using ArrayList 应用程序由于nullpointer异常而崩溃 - App crashing due to nullpointer exception 在我的代码中获取nullpointer异常 - Getting nullpointer exception in my code 当我的应用程序未运行时(仅在未运行时),为什么我的BroadcastReceiver给我一个nullpointer异常? - When my app is not running (only when not running), why does my BroadcastReceiver give me a nullpointer exception? 作为Interface中的参数的Arraylist在Android RecyclerViewAdapter中出现NullPointer Exception - Arraylist as argument in Interface occur NullPointer Exception in Android RecyclerViewAdapter 棉花糖以下的android版本中的Arraylist抛出Nullpointer异常 - Arraylist throwing Nullpointer exception in android versions below Marshmallow 在Android上恢复应用程序时出现NullPointer异常 - NullPointer exception when resuming app on android 运行应用程序时获取NullPointer异常 - When Run The App Get NullPointer Exception 我的Android TabHost崩溃将NullPointer异常 - My Android TabHost Is Crashing Will NullPointer Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM