我在GUI中制作了一个程序,用于存储学生的数据。一切似乎都正常。但是,当我输入课程名称时,它会返回空指针异常。我能够理解确切的问题是什么。它输入的一切都很好,除了我在Assi1类中的课程代码之后的代码行中注释的课程名称之外。
package assi1;
import javax.swing.*;
import java.util.*;
import java.lang.*;
class Student {
private String name;
private int cms;
public Student() {
this.name = "none";
this.cms = 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCms() {
return cms;
}
public void setCms(int cms) {
this.cms = cms;
}
}//end of Student class
class Courses {
private int crs;
private String crsname;
public Courses() {
this.crs = 0;
this.crsname = "none";
}
public int getCrs() {
return crs;
}
public void setCrs(int crs) {
this.crs = crs;
}
public String getCrsname() {
return crsname;
}
public void setCrsname(String crsname) {
this.crsname = crsname;
}
}//end of courses class
class Marks {
private int smarks, mmarks, fmarks, tmarks;
public Marks() {
this.smarks = 0;
this.mmarks = 0;
this.fmarks = 0;
this.tmarks = 0;
}
public int getSmarks() {
return smarks;
}
public void setSmarks(int smarks) {
this.smarks = smarks;
}
public int getMmarks() {
return mmarks;
}
public void setMmarks(int mmarks) {
this.mmarks = mmarks;
}
public int getFmarks() {
return fmarks;
}
public void setFmarks(int fmarks) {
this.fmarks = fmarks;
}
public int getTmarks() {
return this.smarks + this.mmarks + this.fmarks;
}
}
public class Assi1 {
public static void main(String[] args) {
String ch = null;
int ii = 0, del = 0, last = 0;
//data entry
int size = 0, sizee = 0, cmf = 0, change = 0;
String name = "none", crsname[] = null;
int cms = 0, crs = 1, smarks[], mmarks[], fmarks[];
Courses cr[] = null;
Student std[] = null;
Marks mrks[] = null;
size = Integer.parseInt(JOptionPane.showInputDialog(null, "enter the number of students You want to enroll"));
std = new Student[size];
for (int i = 0; i < size; i++) {
std[i] = new Student();
std[i].setName(name = JOptionPane.showInputDialog(null, "Enter the name of Student"));
std[i].setCms(cms = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the CMSID of Student")));
sizee = Integer.parseInt(JOptionPane.showInputDialog(null, "enter the number of courses You want to enroll"));
cr = new Courses[sizee];
mrks = new Marks[sizee];
crsname = new String[sizee];
smarks = new int[sizee];
mmarks = new int[sizee];
fmarks = new int[sizee];
for (int j = 0; j < sizee; j++) {
cr[i].setCrsname(crsname[j] = JOptionPane.showInputDialog(null, "Enter the course Name")); //This the line which gives Null Pointer Exception
mrks[i].setSmarks(smarks[j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Sessional Marks")));
mrks[i].setMmarks(mmarks[j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Mid term Marks")));
mrks[i].setFmarks(fmarks[j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Final term Marks")));
}
}
//data entry
System.out.println();
//for viewing
ch = JOptionPane.showInputDialog(null, "Press v to view the Information of Student without full marks\n Press u to update Information of Student\n"
+ "Press d to delete the Information of Student\n Press L to list all the enrolled Students\n"
+ "Press LM to list all the Students with thier marks and selected courses\n Press s to list the information of a Single Student with total marks\n"
+ "Press x to exit");
if (ch.equals("v") || ch.equals("V")) {
cmf = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the CMSID of Student"));
while (std[ii] != null && std[ii].getCms() != cmf) {
ii++;
}
change = ii;
JOptionPane.showMessageDialog(null, "The name of Student " + std[change].getName() + "\n" + "The CMSID of Student " + std[change].getCms()
+ "\n" + "The Course name " + cr[change].getCrsname() + "\n" + "The number Courses " + cr[change].getCrs() + "\n" + "The Sessional Marks " + mrks[change].getSmarks()
+ "\n" + "The Mid Term Marks " + mrks[change].getMmarks() + "\n" + "The Final Term Marks " + mrks[change].getFmarks());
} else if (ch.equals("u") || ch.equals("U")) { //for update
cmf = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the CMSID of Student"));
while (std[ii] != null && std[ii].getCms() != cmf) {
ii++;
}
change = ii;
std[change].setName(name = JOptionPane.showInputDialog(null, "Enter the name of Student"));
std[change].setCms(cms = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the CMSID of Student")));
cr[change].setCrs(crs = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of courses")));
crsname = new String[crs];
smarks = new int[crs];
mmarks = new int[crs];
fmarks = new int[crs];
for (int j = 0; j < crs; j++) {
cr[change].setCrsname(crsname[j] = JOptionPane.showInputDialog(null, "Enter the course Name"));
mrks[change].setSmarks(smarks[j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Sessional Marks")));
mrks[change].setMmarks(mmarks[j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Mid term Marks")));
mrks[change].setFmarks(fmarks[j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Final term Marks")));
}
} else if (ch.equals("d") || ch.equals("D")) { //deletion
cmf = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the CMSID of Student"));
while (std[ii] != null && std[ii].getCms() != cmf) {
ii++;
}
change = ii;
std[change] = std[size - 1];
cr[change] = cr[size - 1];
mrks[change] = mrks[size - 1];
std[size - 1] = null;
cr[size - 1] = null;
mrks[size - 1] = null;
} else if (ch.equals("l") || ch.equals("L")) {
for (int i = 0; i < std.length; i++) {
for (int j = 0; j < crs; j++) {
JOptionPane.showMessageDialog(null, "The name of Student " + std[i].getName() + "\n" + " The CMSID of Student " + std[i].getCms()
+ "\n" + "The Course name " + crsname[j]);
}
}
} else if (ch.equals("lm") || ch.equals("LM") || ch.equals("lM") || ch.equals("Lm")) {
for (int i = 0; i < std.length; i++) {
JOptionPane.showMessageDialog(null, "The name of Student " + std[i].getName() + "\n" + "The CMSID of Student " + std[i].getCms()
+ "\n" + "The Course name " + cr[i].getCrsname() + "\n" + "The number Courses " + cr[i].getCrs() + "\n" + "The Sessional Marks " + mrks[i].getSmarks()
+ "\n" + "The Mid Term Marks " + mrks[i].getMmarks() + "\n" + "The Final Term Marks " + mrks[i].getFmarks() + "\n"
+ "The Total Marks " + mrks[i].getTmarks());
}
} else if (ch.equals("s") || ch.equals("S")) {
cmf = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the CMSID of Student"));
while (std[ii] != null && std[ii].getCms() != cmf) {
ii++;
}
change = ii;
for (int j = 0; j < crs; j++) {
JOptionPane.showMessageDialog(null, "The name of Student " + std[change].getName() + "\n" + "The CMSID of Student " + std[change].getCms()
+ "\n" + "The Course name " + crsname[j] + "\n" + "The number Courses " + cr[change].getCrs() + "\n" + "The Sessional Marks " + mrks[change].getSmarks()
+ "\n" + "The Mid Term Marks " + mrks[change].getMmarks() + "\n" + "The Final Term Marks " + mrks[change].getFmarks() + "\n"
+ "The Total Marks " + mrks[change].getTmarks());
}
} else if (ch.equals("x") || ch.equals("X")) {
JOptionPane.showMessageDialog(null, "Program Executed!");
} else {
JOptionPane.showMessageDialog(null, "Error");
}
}
}