0

我正在为我的编程模块开发一个学生项目,在这个项目中,我们必须构建一个项目来整合许多不同的 Java 功能,主要是 GUI 设计和高级数据结构,所以我正在开发一个用于 Java 培训学生的系统,他们在那里将在系统中注册,学习不同的 Java 教程并参加考试。我现在停留在其中一个教程上,因为我认为这是一个好主意,而不是让多个页面浏览教程,当用户点击“下一步”按钮或“返回键。因此,所有图像和文本文件的名称都将存储在一个链表中(因此顺序正确),教程将从索引 1(开始)开始,因此将显示图像 1 和内容 1,当用户点击下一个时,索引将增加 1,并且图像和内容将被更新。我目前收到一条错误消息

java.lang.NullPointerException at gui.Tutorial1page1.imageSelect(Tutorial1page1.java:66)
at gui.Tutorial1page1.<init>(Tutorial1page1.java:69)
at gui.Tutorial1page1$1.run(Tutorial1page1.java:39)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

任何关于从哪里开始寻找或什至不可能的建议将不胜感激。可能还应该提到在我添加链接列表之前,整个事情只是在阅读图像。谢谢 :-)

package gui;

import java.awt.EventQueue;
import java.awt.TextArea;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
 import javax.swing.JButton;
import java.awt.Font;
import javax.swing.BorderFactory;
 import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;


public class Tutorial1page1 extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Tutorial1page1 frame = new Tutorial1page1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//declare the linkedlist for the images
private LinkedList<String> imageContent;

//declare int for the index of what image to select
//this will increase by one when user hits next button & decrease for back button
int index = 1;

//store the names of the files in order, add last will add them to end of list
public void Images()
{
    imageContent = new LinkedList<String>();
    imageContent.addLast("\"IfStatement.png\"");
    imageContent.addLast("\"IfElseStatement.png\"");
}

//select image
public String imageSelect(int index)
{
return imageContent.get(index);
}

String image = imageSelect(index);
/**
 * Create the frame.
 * @throws IOException 
 */
public Tutorial1page1() throws IOException {

    //create, format and locate jframe
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit program when framed closed
    setBounds(300, 75, 800, 600);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    setContentPane(contentPane);
    contentPane.setLayout(null);

    //create, format and position button for going back through pages of tutorial
    //should this be disabled on first page??
    JButton btnBack = new JButton(" < Back ");
    btnBack.setForeground(Color.WHITE);
    btnBack.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnBack.setBackground(Color.BLUE);
    btnBack.setBounds(150,500,120,30);
    contentPane.add(btnBack);

    //create, format and position button for quitting the tutorial
    JButton btnQuit = new JButton("Quit");
    btnQuit.setForeground(Color.WHITE);
    btnQuit.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnQuit.setBackground(Color.BLUE);
    btnQuit.setBounds(350,500,120,30);
    contentPane.add(btnQuit);

    //add button listener for Next button
    btnQuit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            dispose();//close current screen
            //open main screen
            try {
                Main frame = new Main(); 
                //open last page of tutorial, in this case main module page
                frame.setVisible(true);
                //open in centre of screen
                frame.setLocationRelativeTo(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    //create, format and position button for going forward through pages of tutorial
    JButton btnNext = new JButton("Next > ");
    btnNext.setForeground(Color.WHITE);
    btnNext.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnNext.setBackground(Color.BLUE);
    btnNext.setBounds(550,500,120,30);
    contentPane.add(btnNext);

    //add button listener for Next button
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            dispose();//close current screen
            //open main screen
            try {
                Tutorial1page2 frame = new Tutorial1page2(); //open next page of tutorial
                frame.setVisible(true);
                //open in centre of screen
                frame.setLocationRelativeTo(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    //create, format and position help button on each page of tutorial
    //uses label appearance method to look like linked text
    JButton btnHelp = new JButton("Help");
    btnHelp.setForeground(Color.WHITE);
    btnHelp.setBackground(Color.BLUE);
    btnHelp.setFont(new Font("Tahoma", Font.BOLD, 15));
    btnHelp.setBounds(740,0,50,50);
    contentPane.add(btnHelp);
    Login.labelAppearance(btnHelp);

    //import and position resized icon
    BufferedImage iconresized = ImageIO.read(this.getClass().getResource("/gui/iconresized.png"));
    JLabel picLabel = new JLabel(new ImageIcon(iconresized));
    contentPane.add(picLabel);
    picLabel.setBounds(5,10,50,80);

    //create, format and position label for the Tutorial description
    //this will remain the same on all pages of this tutorial
    JLabel lblTutorial1 = new JLabel("TUTORIAL 1 - Composition : If, switch, while & for statements");
    lblTutorial1.setFont(new Font("Gisha", Font.BOLD, 22));
    lblTutorial1.setForeground(Color.BLUE);
    lblTutorial1.setBounds(60, 40, 675, 44);
    lblTutorial1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
    contentPane.add(lblTutorial1);

    //create, format and position label for the title of this page
    //this changes on each page of each tutorial
    JLabel lblIntroductionT1 = new JLabel("Introduction - Select Statements");
    lblIntroductionT1.setFont(new Font("Gisha", Font.ITALIC, 15));
    lblIntroductionT1.setForeground(Color.RED);
    lblIntroductionT1.setBounds(20, 125, 300, 26);
    contentPane.add(lblIntroductionT1);


    //create new text field for displaying notes on the page
    //this will wrap text to the text field horizontally, but allow a vertical scroll bar
    //if the text goes on too long to fit the field vertically
    TextArea Content1 = new TextArea("", 4, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
    Content1.setBounds(20, 155, 400, 300);
    contentPane.add(Content1);
    //add(textField, BorderLayout.NORTH);
    Content1.setEditable(false);

    //read in text file for content
    FileReader readtextfile = new FileReader("src/gui/Tutorial1content.txt");

    //open scanner
    Scanner FileReaderScan = new Scanner(readtextfile);

    //create sting Tut1content to store what is read in
    String Tut1content ="";

    //while there is another line in text file add it to string
    while(FileReaderScan.hasNextLine()){
        String temp = FileReaderScan.nextLine() + "\n";
        Tut1content = Tut1content + temp;

    }

    //add contents of string to the text area
    Content1.append(Tut1content);

    //close scanner
    FileReaderScan.close();


    //import and position the diagram to go with text
    //both should be parallel to each other

    //read in image
    BufferedImage IfState = ImageIO.read(this.getClass().getResource(imageSelect(index)));

    //set image in JLabel
    JLabel picLabel2 = new JLabel(new ImageIcon(IfState));
    //add border to picture
    picLabel2.setBorder(BorderFactory.createLineBorder(Color.black));
    //add to frame
    contentPane.add(picLabel2);
    //set bounds, location
    picLabel2.setBounds(450,155,300,300);

    //create and position progress bar so user can track where they are in tutorial

    JProgressBar tut1progressBar = new JProgressBar(0,5);
    tut1progressBar.setValue(1); //page 1 of 5
    tut1progressBar.setBounds(220, 550, 380, 14);
    contentPane.add(tut1progressBar);

    //add label to progress bar to show page number currently on
    JLabel lblProgress1 = new JLabel("Page 1/5");
    lblProgress1.setFont(new Font("Gisha", Font.ITALIC, 10));
    lblProgress1.setForeground(Color.RED);
    lblProgress1.setBounds(690, 540, 80, 26);
    contentPane.add(lblProgress1);
}
}
4

1 回答 1

0

如果我弄错了,请纠正我,但是在您的代码中,我没有看到您调用该Images方法,这是实例化LinkedList<T>并填充它的方法。.get由于您正在调用null参考,因此您将获得一个NullPointerException.

另一方面,请在您的代码中遵循 Java 命名约定(因此方法名称和变量应采用camelCase 格式)。

于 2013-04-23T18:32:06.623 回答