gpt4 book ai didi

java - 根本无法获取要操作的对象的数组(或 vector )

转载 作者:行者123 更新时间:2023-12-01 14:27:58 25 4
gpt4 key购买 nike

enter image description here我一直在开发一个用于娱乐用途的程序,并且我已经完成了所有布局,只需要完成输入某些值的进度。但是,每当我尝试输出或对调用的数组执行任何操作时,我都会收到空指针异常。这段代码是最新的,我尝试了几种不同的方法,但没有一个起作用。我迫切需要帮助。

代码到我的主目录。

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;
}
}

最佳答案

该数组中没有任何内容。其中的每个元素都是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());
}

关于java - 根本无法获取要操作的对象的数组(或 vector ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17036094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com