我一直致力于一个娱乐用途的计划,我已经完成了布局,只需要完成对某些值的影响。但是,每当我尝试输出或执行任何调用我的数组的操作时,我都会一直得到一个空指针异常。这段代码是最新的,我尝试了几种不同的方法,但没有一种方法有效。我迫切需要帮助。
我的主要代码。
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;
}
}
PlayerInput:
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;
}
}
答案 0 :(得分: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());
}
答案 1 :(得分:0)
我认为它应该是x<CLASSSIZE1
或x<=CLASSSIZE1
,取决于您的要求。
for (int x = 0; x<CLASSSIZE1; x++)
{
robbieArray[x].setChampName(inputRobbieChamps.next());
robbieArray[x].setRole(inputRobbieChamps.next());
robbieArray[x].setTier(inputRobbieChamps.nextInt());
}
答案 2 :(得分:0)
我认为robbieArray [0] .getChampName()导致错误。在for循环中,你正在做x&gt; CLASSSIZE1,CLASSSIZE1的起始值是1.所以它永远不会进入for循环并直接尝试执行robbieArray [0] .getChampName()。 rebiieArray [0]为null,因为没有对象,所以这个调用会导致空指针。
您需要根据自己的要求纠正这种情况。