Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Newbie needing help

meddocmeddoc Member Posts: 1
I have the following code (still underdevelopment) for a school project. The part that is giving me problems is related to the search for the test name within the file.
"
int spot = TestListIn.indexOf(new Tests(null,null,tempTest));
"

The generated error is:

Exception in thread "main" java.lang.NullPointerException

at thalas.Tests.equals(Tests.java:43)
at java.util.ArrayList.indexOf(ArrayList.java:216)

I am not sure what I am doing wrong - any help would be appreciated.

The following is the code:

package thalas;

import java.io.*;
import java.util.*; // required for ArrayList

public class Manipulate_Tests
{
// method for adding a new Student to the list
public static void addTest(ArrayList TestListIn)
{
Integer flag;
String tempTest;
String tempcategory;
String tempTestId;
char choice;

Scanner keyboard = new Scanner(System.in);
keyboard.useDelimiter("
");
System.out.print("Enter Test Name or QUIT: ");
tempTest = keyboard.next();
//change Test name to uppercase
tempTest = tempTest.toUpperCase();

//integer to use as Flag to compare entry with QUIT
int comparison;

//Call object to compare entry with QUIT
Common_snippets Check_entry = new Common_snippets();
comparison = Check_entry.Check_if_stop(tempTest);
System.out.println(comparison);

//to check whether Test already exists in file
int spot = TestListIn.indexOf(new Tests(null,null,tempTest));
System.out.println(spot);
if (spot>=0) {
spot = 1;
}
else {
spot = 0;
}

/*

* comparison = 0 then quit
* spot = 0 then test name does not exist in file
* system uses binary coding (flag) for choice

*/
flag = spot + comparison*2;

switch (flag){

case 0:
case 3: System.out.println();
break;
case 1:System.out.println("Test: "+tempTest+ " already exists ");
addTest(TestListIn);
break;
case 2:

System.out.println("Enter Test Category");
System.out.println("1. Haematology");
System.out.println("2. Biochemistry");
System.out.println("3. Immunology");
System.out.println("4. Imaging");
System.out.println("5. Others");

choice = keyboard.next().charAt(0);
// choice = EasyScanner.nextChar();
System.out.println();
tempcategory = "";
switch(choice)
{
case '1' : tempcategory = "Haematology";
break;
case '2' : tempcategory = "Biochemistry";
break;
case '3' : tempcategory = "Immunology";
break;
case '4' : tempcategory = "Imaging";
break;
case '5' : tempcategory = "Others";
break;
default : System.out.print
("
Please choose a number from 1 - 5 only
");
}
// }while(choice != '5');

//tempcategory = tempcategory1;
System.out.print("Enter Test ID no ");
tempTestId = keyboard.next();

TestListIn.add(new Tests(tempTestId,tempcategory,tempTest ));
}

}

private static void printline()
{


for(int i = 1; i<55; i++)
{System.out.print("*");
}
System.out.print("*");
System.out.print("
");
}
// method for listing details of all Tests in the list

//public static void listAll(ArrayList<Tests> TestListIn)
public static void listAll(ArrayList TestListIn)

{
System.out.println("");

Common_snippets.printline(55);
//printline();
//System.out.println();
String format = "* %1$-15s* %2$-20s* %3$-10s*
";
System.out.format(format, "Test ID", "Category" ,"Test");
Common_snippets.printline(55);
//printline();
for(Tests item : TestListIn)
{

System.out.format(format ,item.get_Test_id(),
item.get_category(),
item.get_Test());
}
Common_snippets.printline(55);
//printline();
//System.out.println();
}
// method for writing the file
public static void writeList(ArrayList TestListIn)
{
try
{
FileOutputStream MemberFile = new FileOutputStream("Test.obf");
ObjectOutputStream Teststream = new ObjectOutputStream(MemberFile);

for (Tests item : TestListIn)
{
Teststream.writeObject(item);
}
Teststream.close();
}
// handle the exception thrown by the FileWriter methods
catch(IOException e)
{
System.out.println("There was a problem writing the file");
}
}
// method for reading the file
public static void readList(ArrayList TestListIn)
{
Tests tempTest;
boolean endOfFile = false;
try
{
// create a FileInputStream object, Test
FileInputStream TestFile = new FileInputStream("Test.obf");
// create an ObjectInputStream object to wrap around Test
ObjectInputStream Teststream = new ObjectInputStream(TestFile);
// read the first (whole) object with the readObject method
tempTest = (Tests) Teststream.readObject();
while (endOfFile != true)
{
try
{
TestListIn.add(tempTest);
// read the next (whole) object
tempTest = (Tests) Teststream.readObject();
}
catch (EOFException e)
{
endOfFile = true;
}

}

Teststream.close();
}
/* handle the exception that is thrown by the FileReader
constructor if the file is not found */
catch(FileNotFoundException e)
{
System.out.println("
No file was read");
}

catch (ClassNotFoundException e) // thrown by readObject
{
System.out.println("
Trying to read an object of an unknown class");
}

catch (StreamCorruptedException e) // thrown by the constructor
{
System.out.println("
Unreadable file format");
}

// handle the exception thrown by the FileReader methods
catch(IOException e)
{
System.out.println("
There was a problem reading the file");
}
}

}



And this is the tests class


package thalas;


import java.io.*;
public class Tests implements Serializable
{
private String Test;
private String category;
private String Test_id;

public Tests (String Test_idIn, String categoryIn, String TestIn )
{
Test = TestIn;
category= categoryIn;
Test_id = Test_idIn;
}

public String toString()
{
return ("Test id "+Test_id+ "
" + "Category "+ category +"
"
+ "Test = "+ Test ) ;
}

public String get_Test()
{
return Test;
}

public String get_category()
{
return category;
}

public String get_Test_id()
{
return Test_id;
}



public boolean equals( Object other )
{
return get_Test_id().equals( ((Tests)other).get_Test_id() );
}

}
Sign In or Register to comment.