Java中ArrayList的空指针异常 (Lib GDX)

huangapple go评论112阅读模式
英文:

Java Nullpointer Exception in ArrayList (Lib GDX)

问题

I have a problem with a "Nullpointer Exception" in LibGDX.
When calling the method "public static ArrayList getList(Stage stage, String className)," I get the Nullpointer Exception.

Would be very grateful for help...

Here the function:

  1. public static ArrayList<BaseActor> getList(Stage stage, String className)
  2. {
  3. ArrayList<BaseActor> list = new ArrayList<BaseActor>();
  4. Class theClass;
  5. theClass=null;
  6. try
  7. {
  8. theClass = Class.forName(className);
  9. }
  10. catch (Exception error)
  11. {
  12. error.printStackTrace();
  13. }
  14. for (Actor a : stage.getActors())
  15. {
  16. if ( theClass.isInstance( a ) )
  17. list.add( (BaseActor)a );
  18. }
  19. return list;
  20. }

Here is the call to the function:

  1. public void update(float dt)
  2. {
  3. try{
  4. for (BaseActor rockActor : BaseActor.getList(mainStage, "Rock"))
  5. turtle.preventOverlap(rockActor);
  6. }
  7. catch (Exception error)
  8. {
  9. error.printStackTrace();
  10. }
  11. }

Java中ArrayList的空指针异常 (Lib GDX)

英文:

I have a problem with a "Nullpointer Exception" in LIb GDX.
When calling the method " public static ArrayList<BaseActor> getList(Stage stage, String className)"
I get the Nullpointer Exception.

Would be very grateful for help...

Java中ArrayList的空指针异常 (Lib GDX)

Here the function:

  1. public static ArrayList&lt;BaseActor&gt; getList(Stage stage, String className)
  2. {
  3. ArrayList&lt;BaseActor&gt; list = new ArrayList&lt;BaseActor&gt;();
  4. Class theClass;
  5. theClass=null;
  6. try
  7. {
  8. theClass = Class.forName(className); }
  9. catch (Exception error)
  10. { error.printStackTrace(); }
  11. for (Actor a : stage.getActors())
  12. {
  13. if ( theClass.isInstance( a ) )
  14. list.add( (BaseActor)a );
  15. }
  16. return list;
  17. }

Here is the call to the function:

  1. public void update(float dt)
  2. {
  3. try{
  4. for (BaseActor rockActor : BaseActor.getList(mainStage, &quot;Rock&quot;))
  5. turtle.preventOverlap(rockActor);
  6. }
  7. catch (Exception error)
  8. { error.printStackTrace(); } ......

...

答案1

得分: 1

这是因为try/catch的结果可能导致theClass仍然为null。 处理这种情况的一种方法是在try中包含您的列表逻辑,像这样:

  1. try
  2. {
  3. theClass = Class.forName(className);
  4. for (Actor a : stage.getActors())
  5. {
  6. if (theClass.isInstance(a))
  7. list.add((BaseActor)a);
  8. }
  9. }
  10. catch (Exception error)
  11. { error.printStackTrace(); }
  12. return list;

而且Chaosfire是对的;您可能需要做的不仅仅是打印堆栈跟踪。

英文:

This is happening because the result of the try/catch could result in theClass still being null. One way to handle this would be to include your list logic inside of the try, like this:

  1. try
  2. {
  3. theClass = Class.forName(className);
  4. for (Actor a : stage.getActors())
  5. {
  6. if ( theClass.isInstance( a ) )
  7. list.add( (BaseActor)a );
  8. }
  9. }
  10. catch (Exception error)
  11. { error.printStackTrace(); }
  12. return list;

And Chaosfire is right; you may need to do more than just print the stack trace.

huangapple
  • 本文由 发表于 2023年4月10日 20:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定