0

在此处输入图像描述我一直在开发一个用于娱乐用途的程序,我已经完成了布局,只需要完成估算某些值的进度。但是,每当我尝试输出或调用我的数组时,我都会不断收到空指针异常。这段代码是最新的,我尝试了几种不同的方法,但都没有奏效。我迫切需要帮助。

代码到我的主要。

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.beans.*;

public class DazzleMain 
{

    public static final int CLASSSIZE1 = 1;//subject to change
    private static PlayerInput[] robbieArray = new PlayerInput [CLASSSIZE1];
    public static LaunchWindows lws = new LaunchWindows();
    //  public static LoadWindow lw1 = new LoadWindow();
    //  public static PlayerShow ps = new PlayerShow ();
    //  public static NewWindow nw = NewWindow ();
    //  public static LoadCompWindow pcw = LoadCompWindow();

    //PlayerInput player = new PlayerInput();

    public static void main(String[] args) throws IOException, FileNotFoundException
    {   
        System.out.println(CLASSSIZE1);

        Scanner inputRobbieChamps = new Scanner (new  FileReader("robbie_mccarthy_champs.txt"));

        for (int x = 0; x>CLASSSIZE1; x++)
        {
            robbieArray[x].setChampName(inputRobbieChamps.next());
            robbieArray[x].setRole(inputRobbieChamps.next());
            robbieArray[x].setTier(inputRobbieChamps.nextInt());
        }
        //inputRobbieChamps.close();

        //  for (int x = 0;x>CLASSSIZE1; x++)
        //{
            System.out.println("The champ name is " + robbieArray[0].getChampName());     //+ "with a role of " + robbieArray[0].getRole() + " and a tier value of " + robbieArray[0].getTier());
        //  }
    }

    public static PlayerInput[] getArray()
    {
        return robbieArray;
    }
}

我的日期文件(robbie_mccarthy_champs.txt):

test best 2
bear chair 3

我的窗口:

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.beans.*;



public class LaunchWindows extends JFrame
{
    public JFrame launchFrame;

    public LaunchWindows ()
    {
        frame();
    }

    public void main(String[] args)
    {

    }

    private void frame()
    {
        launchFrame = new JFrame();
        //    launchFrame.setSize(900,300);
        launchFrame.setVisible(true);
        launchFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        launchFrame.setLayout(new GridLayout(2,4));
        getLaunchFrame().setBounds(100, 300, 900, 300);

        JLabel blankL1 = new JLabel(" ", SwingConstants.CENTER);

        JLabel blankL2 = new JLabel(" ", SwingConstants.CENTER);

        JLabel titleDL = new JLabel("Dazzle ", SwingConstants.RIGHT);
        titleDL.setFont(new Font ("Casteliar", Font.PLAIN, 36));

        JLabel titleSL = new JLabel (" Squad", SwingConstants.LEFT);
        titleSL.setFont(new Font ("Casteliar", Font.PLAIN, 36));

        JButton exitB = new JButton("Exit");
        exitB.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                System.exit(0);
            }
        });

        JButton loadCompB = new JButton("Load Compositions");

        JButton newB = new JButton("New Team Composition Entry");
        newB.addMouseListener(new MouseAdapter() 
        {
            @Override
            public void mouseClicked(MouseEvent e) 
            {
                //getLaunchFrame().setVisible(false);
            }
        });


        JButton loadB = new JButton("Load Player Champions");
        loadB.addMouseListener(new MouseAdapter() 
        {
            @Override
            public void mouseClicked(MouseEvent e) 
            {
            //    launchFrame.setVisible(false);
                //DazzleMain.lw1.getLoadFrame().setVisible(true);
            }
        });


        launchFrame.getContentPane().add(blankL1);
        launchFrame.getContentPane().add(titleDL);
        launchFrame.getContentPane().add(titleSL);
        launchFrame.getContentPane().add(blankL2);

        launchFrame.getContentPane().add(newB);
        launchFrame.getContentPane().add(loadCompB);
        launchFrame.getContentPane().add(loadB);
        launchFrame.getContentPane().add(exitB);

        launchFrame.setTitle("DAZZLERZ");
    }    

    /*********************************************************/

    public JFrame getLaunchFrame() {
        return launchFrame;
    }

    public void setLaunchFrame(JFrame launchFrame) {
        this.launchFrame = launchFrame;
    }
}

玩家输入:

    public class PlayerInput
{
    /*******************************************/
    //vars

//  private String realName;
    private String champName;
    private String role;
    private int tier;


    /******************************************/
    //methods

    public PlayerInput ()
    {
    //  realName = " ";
        champName = " ";
        role = " ";
        tier = 0;
    }

    public PlayerInput (String cN, String r, int t)
    {
    //  realName = rN;
        champName = cN;
        role = r;
        tier = t;
    }

/*  public void setRealName(String rN)
    {
        realName = rN;
    }*/

    public void setChampName (String cN)
    {
        champName = cN;
    }

    public void setRole (String r)
    {
        role = r;
    }

    public void setTier (int t)
    {
        tier = t;
    }

/*      public String getRealName ()
    {
        return realName;
    } */

    public String getChampName ()
    {
        return champName;
    }

    public String getRole ()
    {
        return role;
    }

    public int getTier ()
    {
        return tier;
    }
}       
4

3 回答 3

1

你在那个数组中没有任何东西。其中的每个元素都是null.

因此,当您尝试这样做时:

robbieArray[x].setChampName(inputRobbieChamps.next());

Java 将其视为:

null.setChampName(inputRobbieChamps.next());

...这是不合法的。

您需要做的是在对其执行操作之前实例化对象。

for (int x = 0; x < CLASSSIZE1; x++) {
    robbieArray[x] = new PlayerInput();
    robbieArray[x].setChampName(inputRobbieChamps.next());
    robbieArray[x].setRole(inputRobbieChamps.next());
    robbieArray[x].setTier(inputRobbieChamps.nextInt());
}
于 2013-06-11T04:40:30.520 回答
0

我认为 robbieArray[0].getChampName() 导致错误。在 for 循环中,您正在执行 x>CLASSSIZE1 并且 CLASSSIZE1 的起始值为 1。因此它永远不会进入 for 循环并直接尝试执行 robbieArray[0].getChampName()。rebiieArray[0] 为空,因为其中没有对象,因此此调用导致空指针。

您需要根据您的要求纠正这种情况。

于 2013-06-11T04:56:20.597 回答
0

我确实认为应该是x<CLASSSIZE1or x<=CLASSSIZE1,这取决于您的要求。

for (int x = 0; x<CLASSSIZE1; x++)
        {
            robbieArray[x].setChampName(inputRobbieChamps.next());
            robbieArray[x].setRole(inputRobbieChamps.next());
            robbieArray[x].setTier(inputRobbieChamps.nextInt());
        }
于 2013-06-11T03:57:28.027 回答