所以我对Java很新,在完成AP课程后我目前正在进行一项独立研究。为了创建文本冒险,我决定使用ArrayLists来存储玩家选择和参与的每个“StoryEvent”。当你看到代码时会更有意义,但基本上每当我将StoryEvent对象添加到我的ArrayList列表时按照他们应该的规模增长。然而,每当从MouseClicked()方法引用ArrayList时,ArrayList以某种方式变为大小0.我使用驱动程序调用所有这些,并使用单独的类来创建StoryEvents,但相关代码将遵循。您需要在此处查看并遵循的问题是eventList。
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Adventure_Chapter1 implements MouseListener
{
// storyline based strings/buttons
static String storyLineStart = ("StoryLineStart");
static String storyOptionOne = ("Assassin Option");
static String storyOptionTwo = ("Ranger Option");
static String storyOptionThree = ("Druid Option");
static String storyOptionFour = ("Wizard Option");
static String storyOptionFive = ("Paladin Option");
static String storyOptionSix = ("Berserker Option");
boolean success = true;
JTextArea storyLineDisplay = new JTextArea(storyLineStart);
JTextArea optionOne = new JTextArea(storyOptionOne);
JTextArea optionTwo = new JTextArea(storyOptionTwo);
JTextArea optionThree = new JTextArea(storyOptionThree);
JTextArea optionFour = new JTextArea(storyOptionFour);
JTextArea optionFive = new JTextArea(storyOptionFive);
JTextArea optionSix = new JTextArea(storyOptionSix);
JPanel totalGUI;
JLabel title, chapter;
JButton buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive, buttonSix;
static int choice = 1;
ArrayList<StoryEvent> currentCharacter = new ArrayList<StoryEvent>();
ArrayList<StoryEvent> eventList = new ArrayList<StoryEvent>();
public JPanel createContentPane() {
JLabel chapter = new JLabel ("ESCAPE FROM AGROK KEEP");
//creates JLabel to be the chapter title
chapter.setSize(350, 30);
chapter.setLocation(465, 40);
totalGUI.add(chapter);
//storyLineDisplay JTextArea
storyLineDisplay.setLocation(50, 100);
storyLineDisplay.setSize(1170, 450);
storyLineDisplay.setVisible(true);
storyLineDisplay.setEditable(false);
storyLineDisplay.setLineWrap(true);
storyLineDisplay.setWrapStyleWord(true);
totalGUI.add(storyLineDisplay);
JButton buttonOne = new JButton("Option One"); //creates the buttons for a player to use and
buttonOne.setLocation(50, 600); // JTextAreas to display options
buttonOne.setSize(110, 50);
buttonOne.addMouseListener(this);
totalGUI.add(buttonOne);
optionOne.setLocation(210, 600);
optionOne.setSize(600,50);
optionOne.setVisible(true);
optionOne.setEditable(false);
optionOne.setLineWrap(true);
optionOne.setWrapStyleWord(true);
totalGUI.add(optionOne);
JButton buttonTwo = new JButton("Option Two");
buttonTwo.setLocation(50, 660);
buttonTwo.setSize(110, 50);
buttonTwo.addMouseListener(this);
totalGUI.add(buttonTwo);
optionTwo.setLocation(210, 660);
optionTwo.setSize(600, 50);
optionTwo.setVisible(true);
optionTwo.setEditable(false);
optionTwo.setLineWrap(true);
optionTwo.setWrapStyleWord(true);
totalGUI.add(optionTwo);
JButton buttonThree = new JButton("Option Three");
buttonThree.setLocation(50, 720);
buttonThree.setSize(110, 50);
buttonThree.addMouseListener(this);
totalGUI.add(buttonThree);
optionThree.setLocation(210, 720);
optionThree.setSize(600, 50);
optionThree.setVisible(true);
optionThree.setEditable(false);
optionThree.setLineWrap(true);
optionThree.setWrapStyleWord(true);
totalGUI.add(optionThree);
JButton buttonFour = new JButton("Option Four");
buttonFour.setLocation(50, 780);
buttonFour.setSize(110, 50);
buttonFour.addMouseListener(this);
totalGUI.add(buttonFour);
optionFour.setLocation(210, 780);
optionFour.setSize(600, 50);
optionFour.setVisible(true);
optionFour.setLineWrap(true);
optionFour.setWrapStyleWord(true);
optionFour.setEditable(false);
totalGUI.add(optionFour);
JButton buttonFive = new JButton("Option Five");
buttonFive.setLocation(50, 840);
buttonFive.setSize(110, 50);
buttonFive.addMouseListener(this);
totalGUI.add(buttonFive);
optionFive.setLocation(210, 840);
optionFive.setSize(600, 50);
optionFive.setVisible(true);
optionFive.setEditable(false);
optionFive.setLineWrap(true);
optionFive.setWrapStyleWord(true);
totalGUI.add(optionFive);
JButton buttonSix = new JButton("Option Six");
buttonSix.setLocation(50, 900);
buttonSix.setSize(110, 50);
buttonSix.addMouseListener(this);
totalGUI.add(buttonSix);
optionSix.setLocation(210, 900);
optionSix.setSize(600, 50);
optionSix.setVisible(true);
optionSix.setEditable(false);
optionSix.setLineWrap(true);
optionSix.setWrapStyleWord(true);
totalGUI.add(optionSix);
// add stat JLabels for characters here
return totalGUI;
}
public void initiliaze() throws FontFormatException, IOException
{
JFrame adventureFrame = new JFrame("The Fall of Agronak"); //creates JFrame for text adventure
Adventure_Chapter1 demo = new Adventure_Chapter1();
adventureFrame.setContentPane(demo.createContentPane());
adventureFrame.setSize(1280, 1024);
adventureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adventureFrame.setVisible(true);
load(); // loads StoryEvents
play(0);
System.out.println("Init() eventList size: " + eventList.size());
}
private void load()
{
//ArrayList<StoryEvent> loadList = new ArrayList<StoryEvent>();
int x = 0;
switch(x)
{
case 0:
StoryEvent txt0 = new StoryEvent(storyLineStart, storyOptionOne, storyOptionTwo, storyOptionThree, storyOptionFour,
storyOptionFive, storyOptionSix, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6);
eventList.add(txt0);
case 1:
StoryEvent assassinStart = new StoryEvent("Assassin Breaks out of Jail.", "option 1", "option2", "option3",
"option4", "option5", "option6", 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7);
eventList.add(assassinStart);
}
}
public void updatePlayer() // to be implemented - reprint stats and check if dead
{
System.out.println("Player Updated ");
}
public void play(int c) // to be implemented
{
storyLineDisplay.setText("testing");
System.out.println("Play() eventList size:" + eventList.size());
optionOne.setText(eventList.get(c).getOption1());
optionTwo.setText(eventList.get(c).getOption2());
optionThree.setText(eventList.get(c).getOption3());
optionFour.setText(eventList.get(c).getOption4());
optionFive.setText(eventList.get(c).getOption5());
optionSix.setText(eventList.get(c).getOption6());
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource().equals(buttonOne));
{
if (success == true)
{
updatePlayer();
System.out.println("MouseClicked eventList size: " + eventList.size());
//currentCharacter.add(eventList.get(choice));
//choice = eventList.get(choice).getNext1();
play(choice);
}
else
{
updatePlayer();
currentCharacter.add(eventList.get(choice));
choice = currentCharacter.get(currentCharacter.size() -1).getFail1();
}
}
}
因此,基本上我调用initialize(),然后创建所有GUI元素。然后它调用load(),它将我的StoryEvents添加到ArrayList eventList,这是这里的主要问题。 initialize()中eventList的大小为2,以及play()方法中的大小。稍后将使用play()方法来改变JTextAreas以及游戏的相应故事情节选项。然而,只要我点击一个按钮,在这种情况下,我只留下代码,如果我点击buttonOne,并检查eventList的大小我得到0.我几乎无能为力,并试图问co - 学生,以前的学生和我的导师。如果有人能解释为什么eventList变成0大小,以及如何解决这个问题,我将非常感激。如果成功boolean == true,您可以忽略有关的部分。这将是后来游戏的一部分,但目前无关紧要。每当我尝试从MouseClicked或play()中的eventList检索信息时,我得到的错误是IndexOutOfBoundsExceptions。 Eclipse告诉我,在play()中,ArrayList的大小为2,但它在两行之后给出了一个错误。这是输出:
Play() eventList size:2
Init() eventList size: 2
Player Updated
MouseClicked eventList size: 0
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Adventure_Chapter1.mouseClicked(Adventure_Chapter1.java:277)
insert further lines of errors here.
答案 0 :(得分:0)
在您的代码中,我看到了代码
//currentCharacter.add(eventList.get(choice));
被评论。这会导致无法在arrayList中进行stro。
因此代码
currentCharacter.get(currentCharacter.size() -1)
抛出NUllPointer异常
答案 1 :(得分:0)
在我看来,这里有两个问题。一个是你缺少一个void静态主入口点,在那里你打包'UI并启动AWT事件线程。另一个是如何在没有'主'的情况下运行?
您需要将面板添加到JFrame对象中。 转到此处并构建一个JFrame对象。然后将您的面板添加到该“原生”容器中,结果应该会有所改善。
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
答案 2 :(得分:0)
请遵循eventList的路径。
public class Adventure_Chapter1 implements MouseListener
{
//GUI based JObjects here
boolean success = true;
ArrayList<StoryEvent> eventList = new ArrayList<StoryEvent>();
public JPanel createContentPane()
{
//creates GUI
}
public void initiliaze() throws FontFormatException, IOException
{
JFrame adventureFrame = new JFrame("The Fall of Agronak");
Adventure_Chapter1 demo = new Adventure_Chapter1();
adventureFrame.setContentPane(demo.createContentPane());
adventureFrame.setSize(1280, 1024);
adventureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adventureFrame.setVisible(true);
load(); // loads StoryEvents
play(0);
System.out.println("Init() eventList size: " + eventList.size());
}
private void load()
{
//ArrayList<StoryEvent> loadList = new ArrayList<StoryEvent>();
int x = 0;
switch(x)
{
case 0:
StoryEvent txt0 = new StoryEvent(storyLineStart, storyOptionOne, storyOptionTwo, storyOptionThree, storyOptionFour,
storyOptionFive, storyOptionSix, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6);
eventList.add(txt0);
case 1:
StoryEvent assassinStart = new StoryEvent("Assassin Breaks out of Jail.", "option 1", "option2", "option3",
"option4", "option5", "option6", 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7);
eventList.add(assassinStart);
}
}
public void updatePlayer()
{
System.out.println("Player Updated ");
}
public void play(int c) // to be implemented
{
storyLineDisplay.setText("testing");
System.out.println("Play() eventList size:" + eventList.size());
optionOne.setText(eventList.get(c).getOption1());
optionTwo.setText(eventList.get(c).getOption2());
optionThree.setText(eventList.get(c).getOption3());
optionFour.setText(eventList.get(c).getOption4());
optionFive.setText(eventList.get(c).getOption5());
optionSix.setText(eventList.get(c).getOption6());
}
public void mouseClicked(MouseEvent e)
{
if (e.getSource().equals(buttonOne));
{
if (success == true)
{
updatePlayer();
System.out.println("MouseClicked eventList size: " + eventList.size());
play(choice);
}
else
{
updatePlayer();
currentCharacter.add(eventList.get(choice));
choice = currentCharacter.get(currentCharacter.size() -1).getFail1();
}
}
}