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

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

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:

public static ArrayList<BaseActor> getList(Stage stage, String className)
{
    ArrayList<BaseActor> list = new ArrayList<BaseActor>();

    Class theClass;
    theClass=null;

    try
    {
        theClass = Class.forName(className);
    }
    catch (Exception error)
    {
        error.printStackTrace();
    }

    for (Actor a : stage.getActors())
    {
        if ( theClass.isInstance( a ) )
            list.add( (BaseActor)a );
    }

    return list;
}

Here is the call to the function:

public void update(float dt)
{

    try{
        for (BaseActor rockActor : BaseActor.getList(mainStage, "Rock"))
            turtle.preventOverlap(rockActor);
    }
    catch (Exception error)
    {
        error.printStackTrace();
    }
}

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:

 public  static ArrayList&lt;BaseActor&gt; getList(Stage stage, String className)
{
    ArrayList&lt;BaseActor&gt; list = new ArrayList&lt;BaseActor&gt;();

    Class theClass;
    theClass=null;

    try
    {
        theClass = Class.forName(className);  }
    catch (Exception error)
    {  error.printStackTrace();  }

    for (Actor a : stage.getActors())
    {
        if ( theClass.isInstance( a ) )
            list.add( (BaseActor)a );
    }

    return list;
}

Here is the call to the function:

 public void update(float dt)
{

    try{
        for (BaseActor rockActor : BaseActor.getList(mainStage, &quot;Rock&quot;))
            turtle.preventOverlap(rockActor);
    }
    catch (Exception error)
    {  error.printStackTrace();  } ......

...

答案1

得分: 1

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

try
{
    theClass = Class.forName(className);

    for (Actor a : stage.getActors())
    {
        if (theClass.isInstance(a))
            list.add((BaseActor)a);
    }
}
catch (Exception error)
{ error.printStackTrace(); }

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:

try
{
    theClass = Class.forName(className);

    for (Actor a : stage.getActors())
    {
        if ( theClass.isInstance( a ) )
            list.add( (BaseActor)a );
    }
}
catch (Exception error)
{  error.printStackTrace();  }

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:

确定