我正在为我的大学任务做一个Java程序,但有些东西不起作用。我可以被认为是Java的新手,因为我没有太多的编码经验。
{
"notification": {
"messageMD5Sum": "ce8d9520466b3cf9d920a8d9d338f11a",
"messageId": "33e7e635-ecc0-534a-8498-b5a442fb6db6",
"timestamp": "2018-06-06 09:40:11.911"
},
"delivery": {
"deliveryId": "cfeecbcf-8a08-59c2-83f1-985e2434b67f",
"destination": "arn:aws:sns:ap-northeast-1:111122223333:endpoint/APNS_SANDBOX/com.myapp.domain/25b45520-c2fe-3345-9c54-e458798b74ee",
"dwellTimeMs": 37,
"attempts": 1,
"token": "edf0462f0caa6e6783857774009f6266edbaf8e1b65bc1f71409652f2a47be1c"
},
"status": "FAILURE"
}
我想在package javaassignment;
import java.io.*;
import java.util.*;
public class Main extends javax.swing.JFrame
{
private FileInputStream in;
static String name;
Scanner file;
public Main(String filename) throws FileNotFoundException
{
in = new FileInputStream(filename);
try
{
file=new Scanner(new File("c:/users/MSI-USER/desktop/JavaAssignment/Student.txt"));
}
catch(FileNotFoundException ea)
{
ea.printStackTrace();
}
}
public void Read()
{
do
{
name=file.nextLine();
}
while(file.hasNext());
System.out.println(name);
file.close();
}
public String getWord() throws IOException
{
int c;
StringBuffer buf = new StringBuffer();
do
{
c = in.read();
if (Character.isSpace((char) c))
return buf.toString();
else
buf.append((char) c);
} while (c != -1);
return buf.toString();
}
public Main()
{
ComboPanel.add(NameText);
NameText.setEditable(false);
NameText.setText(name);
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ButtonPanel = new javax.swing.JPanel();
PrintButton = new javax.swing.JButton();
SaveButton = new javax.swing.JButton();
CalcButton = new javax.swing.JButton();
LabelPanel = new javax.swing.JPanel();
StudentLabel = new javax.swing.JLabel();
AttendanceLabel = new javax.swing.JLabel();
ScrollPanel = new javax.swing.JPanel();
jScrollBar1 = new javax.swing.JScrollBar();
ComboPanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
PrintButton.setText("Print");
PrintButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PrintButtonActionPerformed(evt);
}
});
SaveButton.setText("Save");
SaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveButtonActionPerformed(evt);
}
});
CalcButton.setText("Calculate Percentage");
CalcButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CalcButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
ButtonPanel.setLayout(ButtonPanelLayout);
ButtonPanelLayout.setHorizontalGroup(
ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ButtonPanelLayout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(SaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PrintButton, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(CalcButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
ButtonPanelLayout.setVerticalGroup(
ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ButtonPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PrintButton, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
.addComponent(CalcButton, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
.addComponent(SaveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
.addGap(21, 21, 21))
);
StudentLabel.setText("Student");
AttendanceLabel.setText("Attendance Status");
javax.swing.GroupLayout ComboPanelLayout = new javax.swing.GroupLayout(ComboPanel);
ComboPanel.setLayout(ComboPanelLayout);
ComboPanelLayout.setHorizontalGroup(
ComboPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
ComboPanelLayout.setVerticalGroup(
ComboPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout ScrollPanelLayout = new javax.swing.GroupLayout(ScrollPanel);
ScrollPanel.setLayout(ScrollPanelLayout);
ScrollPanelLayout.setHorizontalGroup(
ScrollPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ScrollPanelLayout.createSequentialGroup()
.addComponent(ComboPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
ScrollPanelLayout.setVerticalGroup(
ScrollPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
.addComponent(ComboPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
javax.swing.GroupLayout LabelPanelLayout = new javax.swing.GroupLayout(LabelPanel);
LabelPanel.setLayout(LabelPanelLayout);
LabelPanelLayout.setHorizontalGroup(
LabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LabelPanelLayout.createSequentialGroup()
.addComponent(StudentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(AttendanceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
.addComponent(ScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
LabelPanelLayout.setVerticalGroup(
LabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LabelPanelLayout.createSequentialGroup()
.addGroup(LabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(AttendanceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
.addComponent(StudentLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(LabelPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(LabelPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void PrintButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void CalcButtonActionPerformed(java.awt.event.ActionEvent evt) {
JavaAssignment J=new JavaAssignment();
PercentageDialog P=new PercentageDialog();
P.setVisible(true);
}
public static void main(String args[]) throws java.io.IOException
{
try
{
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(() -> {
new Main().setVisible(true);
});
}
private javax.swing.JTextField NameText;
// Variables declaration - do not modify
private javax.swing.JLabel AttendanceLabel;
private javax.swing.JPanel ButtonPanel;
private javax.swing.JButton CalcButton;
private javax.swing.JPanel ComboPanel;
private javax.swing.JPanel LabelPanel;
private javax.swing.JButton PrintButton;
private javax.swing.JButton SaveButton;
private javax.swing.JPanel ScrollPanel;
private javax.swing.JLabel StudentLabel;
private javax.swing.JScrollBar jScrollBar1;
// End of variables declaration
}
中显示外部文件的内容,但是当我运行该程序时,它会一直给我这个错误。
JFrame
有人可以告诉我我做错了什么吗?
答案 0 :(得分:1)
在Java中,java.lang.NullPointerException几乎总是意味着你试图对一个空对象做一些事情 - 一个尚未创建的对象,或一个已创建但未正确初始化的对象。
第57行是:
NameText.setEditable(false);
所以首先要看的是你是否曾创建过NameText。
你把它放得更远:
private javax.swing.JTextField NameText;
请注意,您违反了一个重要的样式原则 - 对象和变量最初应该是小写的,并且类最初应该大写。
请注意,这与自然英语用法相反。
Steve is a person.
在Java中:
Person steve = new Person();
你会习惯的。如果你没有其他人(和你!)在阅读你的代码时会遇到很多困难。
所以NameText应该是nameText。
既然强制性的唠叨已经结束,我可以回到强制性的初始化唠叨:D
你使用它之前必须使用新的NAME。
e.g。
NameText = new JTextField();
请注意,您几乎肯定会遇到所有其他变量的相同问题。
此外,返回到样式唠叨 - 调用Main类使得更难理解发生了什么,因为构造函数看起来太过类似于默认的Java入口点(public static void main(String [] args)的东西这是代码开始执行的起点。)