如何在java中运行具有多个公共类的程序?

时间:2016-02-02 14:03:57

标签: java nullpointerexception

我是java的新手。我写了以下程序:

import java.util.ArrayList;
import java.io.*;
import java.util.*;



public class dotcombust{
    GameHelper helper=new GameHelper();
    ArrayList<Dotcom> dotcomlist;
    int nofguess=0;

    private void setup(){    
        Dotcom one =new Dotcom();
        dotcomlist.add(one);
        Dotcom two= new Dotcom();
        dotcomlist.add(two);
        Dotcom three= new Dotcom();
        dotcomlist.add(three);

        for(Dotcom obj : dotcomlist)
        {
            obj.setname();
            ArrayList<String> loc= helper.placeDotCom(3);
            obj.setlocation(loc);
        }
    }

    private void startplaying(){    
        while(!dotcomlist.isEmpty())
        {
            nofguess++;
            String guess=helper.getUserInput("Enter a guess ");

            checkuser(guess);
        }
        finish();
    }

    private void checkuser(String guess){    
        String result="miss";
        for(Dotcom obj : dotcomlist)
        {
            result=obj.checkyourself(guess);
            if(result.equals("kill"))
            {
                System.out.println("You killed " + obj.name);
                dotcomlist.remove(obj);
                break;
            }
            else if(result.equals("hit"))
            {
                break;
            }
       }

       if(result.equals("hit") || result.equals("miss"))
       {
           System.out.println(result);
       }
    }

    private void finish(){
        System.out.println("You took " + nofguess + "guesses. If they are below 10, congrats. Otherwise you suck balls. No offense.");
    }

    public static void main(String[] args){
        dotcombust game;
        game=new dotcombust();
        game.setup();
        game.startplaying();
    }
}

当我运行程序时,我在dotcombust.setup()和dotcombust.startplaying()语句中得到java.lang.NullPointerException。 我认为Dotcom和Gamehelper类与错误无关,因此没有在这里显示它们。 请帮我解决问题。 另外,我想知道是否可以在不同的源文件中编写Dotcom和Gamehelper类,但在此代码中使用它?如果是这样的话?。

2 个答案:

答案 0 :(得分:2)

您需要实例化dotcomlist。

location / {
    rewrite ^/#/(.*) $1 permanent;
    try_files $uri /index.html;
}

答案 1 :(得分:1)

dotcomlist在setup()

中为null

添加:

dotcomlist= new ArrayList<Dotcom>();

在设置的第1行()