我正在爲我的編程模塊開發一個學生項目,在該項目中,我們必須構建一個項目以合併許多不同的Java功能,主要是GUI設計和高級數據結構,所以我正在爲學生在Java系統中進行培訓,他們將在系統中註冊,在Java中使用不同的教程並參加測試。我現在停留在其中一個教程上,因爲我認爲這將是一個好主意,而不是讓教程中的多個頁面在用戶點擊「下一個」按鈕時教程中的圖像和內容將在該頁面上更新,或者「返回鍵。因此,圖像和文本文件的所有名稱將存儲在鏈接列表中(所以按照正確的順序),並且教程將從索引1(開始)開始,因此將顯示圖像1和內容1,如同用戶點擊下一個索引將增加1,圖像和內容將被更新。 我目前得到的使用鏈接列表讀取Java中的圖像/文本文件
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);
}
}
「從哪裏開始尋找任何建議」 - 我會開始尋找'TutorialPage1.java'的第6行 6。 :) – 2013-04-23 18:27:04
@VivinPaliath:我認爲這是第66行,但你的想法是正確的。 – 2013-04-23 18:28:09
你還沒有調用Images方法初始化你的列表 – prashant 2013-04-23 18:32:12