2017-02-21 4 views
-1

私は、私が設計した「小説」クラスと「雑誌」クラスの属性のテキスト値を入力できるようにするプログラムを用意していますが、問題があります私がactionをトリガするたびにnullポインタの例外を与えるactionListenerと一緒に。私の推測では、それは私が基本的にJButton actionPerformed Nullを与えるメソッド

switch(theList.getSelectedIndex()){ 
       case -1: JOptionPane.showMessageDialog(null, "You did not select an object type to create!", "Error!", JOptionPane.ERROR_MESSAGE); 
         break; 
       case 0: createObject(theMagazine, titleField, authorField, publisherField, publisherLocationField, copyrightYearField, priceEnterField, ISBNEnterField); 
         JOptionPane.showMessageDialog(null, theMagazine.toString(), "This is The Magazine You Created!", JOptionPane.INFORMATION_MESSAGE); 
         break; 
       case 1: createObject(theNovel, titleField, authorField, publisherField, publisherLocationField, copyrightYearField, priceEnterField, ISBNEnterField); 
         theNovel.setHardCover(Boolean.parseBoolean(JOptionPane.showInputDialog(null,"Is this Book a hard cover or not?","Question!",JOptionPane.QUESTION_MESSAGE))); 
         JOptionPane.showMessageDialog(null, theNovel.toString(), "This is The Magazine You Created!", JOptionPane.INFORMATION_MESSAGE); 
         break; 
      } 

が、私はそれが適切な属性値のためのCreateObject()メソッドを呼び出して、その後のJOptionPaneのメッセージダイアログを表示することができている何をしたいのか、switch文を持つとは何かを持っていますこれらのクラスの両方に含まれるtoString()メソッドを使用して作成されたオブジェクトです。問題はこれらのクラスのメソッド構文から来ていないことがわかっているので、問題がどこから来ているのか必ずしも分かりません。すべてのヘルプは大歓迎ですhttp://pastebin.com/HRi9jbSD

:ここでは、エラー・メッセージからテキストがある

import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.event.*; 
import java.awt.*; 
@SuppressWarnings({"serial"}) 
public class GUI extends JFrame{ 

static Magazine theMagazine; 
static Novel theNovel; 

public static void main(String[] args){ 
    final String[] objectTypes = {"Magazine","Novel"}; 
    final Border textFieldBorder = BorderFactory.createLineBorder(Color.black, 1); 
    final Font theFont = new Font("Times New Roman", 0, 25); 
    final JLabel typeSelect = new JLabel("Please select the literature type!"); 
    final JLabel titleEnter = new JLabel("Enter the title here!"); 
    final JLabel authorEnter = new JLabel("Enter the Author here!"); 
    final JLabel publisherEnter = new JLabel("Enter the publisher here!"); 
    final JLabel pubLocationEnter = new JLabel("Enter the Publishers location here"); 
    final JLabel enterCopyrightYear = new JLabel("Enter the copyright year here!"); 
    final JLabel priceEnter = new JLabel("Enter the price here!"); 
    final JLabel ISBNEnter = new JLabel("Enter the ISBN here!");  

    JList<String> theList = new JList<String>(objectTypes); 
    theList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    theList.setLayoutOrientation(JList.HORIZONTAL_WRAP); 
    theList.setVisibleRowCount(-1); 
    theList.setBorder(textFieldBorder); 
    theList.setFont(theFont); 

    JTextField titleField = new JTextField(); 
    titleField.setBorder(textFieldBorder); 
    titleField.setFont(theFont); 
    titleField.setHorizontalAlignment(JTextField.CENTER); 
    JTextField authorField = new JTextField(); 
    authorField.setBorder(textFieldBorder); 
    authorField.setFont(theFont); 
    authorField.setHorizontalAlignment(JTextField.CENTER); 
    JTextField publisherField = new JTextField(); 
    publisherField.setBorder(textFieldBorder); 
    publisherField.setFont(theFont); 
    publisherField.setHorizontalAlignment(JTextField.CENTER); 
    JTextField publisherLocationField = new JTextField(); 
    publisherLocationField.setBorder(textFieldBorder); 
    publisherLocationField.setFont(theFont); 
    publisherLocationField.setHorizontalAlignment(JTextField.CENTER); 
    JTextField copyrightYearField = new JTextField(); 
    copyrightYearField.setBorder(textFieldBorder); 
    copyrightYearField.setFont(theFont); 
    copyrightYearField.setHorizontalAlignment(JTextField.CENTER); 
    JTextField priceEnterField = new JTextField(); 
    priceEnterField.setBorder(textFieldBorder); 
    priceEnterField.setFont(theFont); 
    priceEnterField.setHorizontalAlignment(JTextField.CENTER); 
    JTextField ISBNEnterField = new JTextField(); 
    ISBNEnterField.setBorder(textFieldBorder); 
    ISBNEnterField.setFont(theFont); 
    ISBNEnterField.setHorizontalAlignment(JTextField.CENTER); 

    JButton createObject = new JButton("Create an Object!"); 
    createObject.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent event){ 
      switch(theList.getSelectedIndex()){ 
       case -1: JOptionPane.showMessageDialog(null, "You did not select an object type to create!", "Error!", JOptionPane.ERROR_MESSAGE); 
         break; 
       case 0: createObject(theMagazine, titleField, authorField, publisherField, publisherLocationField, copyrightYearField, priceEnterField, ISBNEnterField); 
         JOptionPane.showMessageDialog(null, theMagazine.toString(), "This is The Magazine You Created!", JOptionPane.INFORMATION_MESSAGE); 
         break; 
       case 1: createObject(theNovel, titleField, authorField, publisherField, publisherLocationField, copyrightYearField, priceEnterField, ISBNEnterField); 
         theNovel.setHardCover(Boolean.parseBoolean(JOptionPane.showInputDialog(null,"Is this Book a hard cover or not?","Question!",JOptionPane.QUESTION_MESSAGE))); 
         JOptionPane.showMessageDialog(null, theNovel.toString(), "This is The Magazine You Created!", JOptionPane.INFORMATION_MESSAGE); 
         break; 
      } 
     } 
    }); 
    JButton closeProgram = new JButton("Close the program!"); 
    closeProgram.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent event){ 
      System.exit(0); 
     } 
    }); 

    JPanel thePanel = new JPanel(); 
    thePanel.setLayout(new GridLayout(9,2)); 

    JFrame theFrame; 

    //first row 
    thePanel.add(typeSelect); 
    thePanel.add(theList); 

    //second row 
    thePanel.add(titleEnter); 
    thePanel.add(titleField); 

    //third row 
    thePanel.add(authorEnter); 
    thePanel.add(authorField); 

    //fourth row 
    thePanel.add(publisherEnter); 
    thePanel.add(publisherField); 

    //fifth row 
    thePanel.add(pubLocationEnter); 
    thePanel.add(publisherLocationField); 

    //sixth row 
    thePanel.add(enterCopyrightYear); 
    thePanel.add(copyrightYearField); 

    //seventh row 
    thePanel.add(priceEnter); 
    thePanel.add(priceEnterField); 

    //eighth row 
    thePanel.add(ISBNEnter); 
    thePanel.add(ISBNEnterField); 

    //ninth row 
    thePanel.add(createObject); 
    thePanel.add(closeProgram); 

    theFrame = new JFrame(); 
    theFrame.add(thePanel); 
    theFrame.setTitle("Literature Creator!"); 
    theFrame.setSize(500,500); 
    theFrame.setVisible(true); 
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    theFrame.setResizable(false); 

} 
public static void createObject(Literature theLiterature, JTextField titleField, JTextField authorField, JTextField publisherField, JTextField publisherLocationField, 
         JTextField copyrightYearField, JTextField priceEnterField, JTextField ISBNEnterField){ 
    theLiterature.setTitle(parseField(titleField)); 
    theLiterature.setAuthor(parseField(authorField)); 
    theLiterature.setPublisher(parseField(publisherField)); 
    theLiterature.setPublisherLocation(parseField(publisherLocationField)); 
    theLiterature.setCopyrightYear(parseNumField(copyrightYearField)); 
    theLiterature.setPrice(parseNumField(priceEnterField)); 
    theLiterature.setISBN(parseNumField(ISBNEnterField)); 
} 
public static String parseField(JTextField field){ 
    if(field.getText() == null){ 
     JOptionPane.showMessageDialog(null, "Error! " + field.getName() + " contains no text!", "Error", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    return field.getText(); 
} 
public static int parseNumField(JTextField field){ 
    if(field.getText() == null){ 
     JOptionPane.showMessageDialog(null, "Error! " + field.getName() + " contains no text!", "Error", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    try{ 
     Integer.parseInt(field.getText()); 
    }catch(NumberFormatException nfe){ 
     JOptionPane.showMessageDialog(null, "Error! Value in field " + field.getName() + " was not a valid number!", "Error!", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    return Integer.parseInt(field.getText()); 
} 

}

これは、ソースコードです!

+1

ここで、 'theMagazine'はどこでヌルに設定されますか? –

+0

@DM haha​​hahahaそれは私がどこにもしなかったように見えるでしょう...おっと、私が推測するばかげたエラーですが、それは長い長い日でした。ありがとうございました! –

答えて

0

デフォルトでは、Javaは新しい定義済みの変数にnullを割り当てます。この静的変数

static Magazine theMagazine; 

は、createObject(...)メソッドに渡すとnullです。

新しいマガジンオブジェクトを作成(または何とか読み込み)し、theMagazine変数に割り当てる必要があります。

+0

ありがとうございます!私は今それを実現する。私はデフォルトのヌル値を認識していましたが、かなり長い時間スキャンしたので、私の脳はそれを再調整していないので、それが問題になることはまだありませんでした..乾杯! –

関連する問題