This is my entire code.. what i do keep getting nullpointerexception error whenever i input 1 or 2 whenever the code is asking me to input the type of display to be shown..
this is my error - "Exception in thread "main" java.lang.NullPointerException
at dakilangtaoako.DakilangTaoAko.alphabetSorter(Dakil angTaoAko.java:195)
at dakilangtaoako.DakilangTaoAko.main(DakilangTaoAko. java:91)
Java Result: 1 "
package dakilangtaoako;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DakilangTaoAko {
public static void main(String[] args){
BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
String mydata[][] = new String[5][3] ;
String inttoword [] = new String [5];
int wordtoint[] = new int [5];
int mychoice = 0;
String toreturn = "";
for (int outside = 0; outside < 5; outside++){
System.out.println("Entry no." + (outside + 1));
for (int inside = 0; inside < 3; inside++){
if(inside == 0){
try {
System.out.print("Name: ");
mydata[outside][inside]= dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.parse("OK"), null, ex);
}
}
else if(inside == 1){
try {
System.out.print("ID: ");
mydata[outside][inside]= dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.parse("OK"), null, ex);
}
}
else if(inside ==2){
try {
System.out.print("Subject: ");
mydata[outside][inside]= dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.parse("OK"), null, ex);
}
}
}
}
for(int outside= 0; outside <5; outside++){
for(int inside = 1; inside <= 3; inside++){
if(inside ==0)
inttoword[outside]=(mydata[outside][inside])+";"+(mydata[outside][inside]);
}
}
for(int outside = 0; outside < 5; outside++){
for(int inside = 1; inside <= 3; inside++){
if(inside ==0)
wordtoint[outside]=Integer.parseInt(mydata[outside][inside]);
}
}
for(int i=0;i<5;i++){
wordtoint[i]=Integer.parseInt(mydata[i][1]);
}
while(mychoice != 5){
System.out.println("-------- DISPLAY OPTIONS --------");
System.out.println("\t [1] Display Entries Alphabetically ");
System.out.println("\t [2] Display Entries by Ascending ID number ");
System.out.println("\t [3] Display Students Enrolled in Prog2 ");
System.out.println("\t [4] Display Students Enrolled in CSO ");
System.out.println("\t [5] Exit ");
System.out.println();
System.out.print("\t Choice:");
try {
mychoice = Integer.parseInt(dataIn.readLine());
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
}
switch (mychoice){
case 1:
System.out.println("----Entries In Alphabetical Order----");
alphabetSorter(inttoword);
for(int limiter = 0; limiter < 5; limiter++)
System.out.println(inttoword[limiter]);
System.out.print("Press [r] to Return to Main Menu...");
try {
toreturn = dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
}
if("r".equals(toreturn))
break;
case 2:
System.out.println(" ---- Entries by Ascending ID Number ----");
for(int outside = 0; outside < 5; outside++){
numSorter(wordtoint);
for(int inside = 0; inside < 5; inside++){
if(Integer.parseInt(mydata[inside][1])== wordtoint[outside])
System.out.println(wordtoint[outside] + ";" + mydata[inside][0]);
}
}
System.out.print("Press [r] to return to main Menu...");
try {
toreturn = dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
}
if("r".equals(toreturn))
break;
case 3:
System.out.println(" ---- Students Enrolled in Prog2 ----");
String mustfind1 = "PROG2";
for(int outside = 0; outside < 5; outside++){
if(mydata[outside][2].equalsIgnoreCase(mustfind1)){
System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
}
}System.out.print("Press [r] to Return to Main Menu...");
try {
toreturn = dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
}
if("r".equals(toreturn))
break;
case 4:
System.out.println(" ---- Students Enrolled in CSO ---- ");
String mustfind2 = "CSO";
for(int outside = 0; outside < 5; outside++){
if(mydata[outside][2].equalsIgnoreCase(mustfind2))
System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
}
System.out.print("Press [r] to Return to Main Menu...");
try {
toreturn = dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
}
if("r".equals(toreturn))
break;
case 5:
mychoice = 5;
break;
default:
System.out.println("Sorry the Number is Not in the Choices!!");
System.out.print("Press [r] to Return to Main Menu...");
try {
toreturn = dataIn.readLine();
} catch (IOException ex) {
Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
}
if("r".equals(toreturn))
break;
} }
}
public static void numSorter(int given[]){
int temp;
for(int outside = 0; outside < given.length-1; outside++)
{
for(int inside = outside+1 ; inside < given.length; inside++)
{
if(given[outside] > given[inside])
{
temp = given[outside];
given[outside]= given[inside];
given[inside]=temp;
}
}
}
}
public static void alphabetSorter(String given[]){
String temp;
for(int outside = 0; outside < given.length -1; outside++)
{
for(int inside = outside +1; inside < given.length; inside++)
{
if(given[outside].compareToIgnoreCase(given[inside])>0)
{
temp= given[outside];
given[outside]= given[inside];
given[inside]=temp;
}
}
}
}
}