Question: JAVA - Can someone decipher the temp is null in the SinglyLinkedList Class in line 33 and make this code run? Thank you very much,
JAVA - Can someone decipher the temp is null in the SinglyLinkedList Class in line 33 and make this code run? Thank you very much, I will upvote! See links below for txt files of additional code that could not fit inside Chegg.


package test;
class SinglyLinkedList
{
private static Node h;
public SinglyLinkedList()
{
h = null;
}
public boolean insert(Student student)
{
if(student == null)
{
return false;
}
Node newStudent = new Node(student);
newStudent.setRecord(student);
newStudent.setNext(null);
Node temp = null;
if(h == null)
{
h = newStudent;
}
else
{
temp = h;
}
while(temp != null && temp.getNext() != null)
{
temp = temp.getNext();
}
temp.setNext(newStudent);
return true;
}
public Student fetch(String queryName)
{
Node temp = h;
while(temp != null)
{
if(temp.getRecord().compareTo(queryName) == 0)
return temp.getRecord();
temp = temp.getNext();
}
return null;
}
public boolean delete(String name)
{
if(h.getRecord().compareTo(name) == 0) {
h = h.getNext();
}
else {
Node curr = h, prev = null;
while(curr != null && curr.getRecord().compareTo(name)!= 0)
{
prev = curr;
curr = curr.getNext();
}
if(curr == null) return false;
prev.setNext(curr.getNext());
}
return true;
}
public boolean update(String queryName, Student updatedStudent)
{
Node temp = h;
while(temp != null && temp.getRecord().compareTo(queryName) != 0)
{
temp = temp.getNext();
}
if(temp == null)
return false;
temp.setRecord(updatedStudent);
return true;
}
public void showAll()
{
Node curr = h;
while(curr != null)
{
System.out.println(curr);
curr = curr.getNext();
}
}
}
package test;
import java.util.Random;
import java.util.Scanner;
public class StudentApp
{
static SinglyLinkedList data = new SinglyLinkedList();
private static int menuChoice = 0; // need to declare static variable
// private static SinglyLinkedList data;
private static void DrawMenu() {
System.out.println("Enter: 1. Insert a new student's information,");
System.out.println(" 2. Fetch and output a student's information,");
System.out.println(" 3. Delete a student's information,");
System.out.println(" 4. Update a student's information,");
System.out.println(" 5. Output all the students information in sorted order");
System.out.println(" 6. Exit the program");
Scanner scan = new Scanner(System.in);
try {
menuChoice = Integer.parseInt(scan.nextLine());
System.out.flush();
} catch (Exception e) {
System.out.flush();
System.out.println("ERROR: Choose a number between 1-6 ");
menuChoice = 0;
return;
}
if (menuChoice == 1) {
Student student = new Student();
student.input();
data.insert(student);
} else if (menuChoice == 2) {
System.out.print("Enter a student name: ");
String nameToSearch = scan.nextLine();
System.out.println(data.fetch(nameToSearch));
} else if (menuChoice == 3) {
System.out.print("Enter a student name: ");
String nameToSearch = scan.nextLine();
System.out.println(data.delete(nameToSearch));
} else if (menuChoice == 4) {
System.out.print("Enter a student name: ");
String nameToSearch = scan.nextLine();
Student studentToChange = data.fetch(nameToSearch);
System.out.println("Current GPA: " + studentToChange.getGpa());
System.out.print("Update GPA: ");
double newGpa = Double.parseDouble(scan.nextLine());
data.delete(nameToSearch);
data.insert(new Student(studentToChange.getName(), studentToChange.getId(), newGpa));
} else if (menuChoice == 5) {
data.showAll();
} else if (menuChoice == 6) {
System.out.println("Thank you!");
} else {
System.out.println("Error");
return;
}
}
public static void main(String[] args)
{
RandomStudents random = new RandomStudents();
Student newStudent = random.getStudent();
data.insert(newStudent);
while (menuChoice != 6)
DrawMenu();
}
}
package test;
import java.util.*;
import java.io.*;
public class RandomStudents
{
// instance variables
private String studentNames[];
private int numberOfNames;
private Random randomInteger;
public RandomStudents()
{
numberOfNames = 1000;
studentNames = new String[numberOfNames*2];
readNamesFromFile(".../boynames.txt", 0, numberOfNames);
readNamesFromFile(".../girlnames.txt", numberOfNames, numberOfNames*2);
long seed = System.currentTimeMillis(); //Get current time as a long.
randomInteger = new Random(seed);//Use seed to generate random numbers.
}
private void readNamesFromFile( String fileName, int start, int end)
{
Scanner inputStream = null;
try
{
inputStream =
new Scanner(new FileInputStream(fileName));
for (int i=start; i
{
String line = inputStream.nextLine();
//Parse out first name, ignore the number
int space = line.indexOf(" ",0);
studentNames[i] = line.substring(0,space);
}
inputStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("File: " + fileName + " not found");
System.out.println("or could not be opened.");
System.exit(0);
}
catch (IOException e)
{
System.out.println("Error reading from file:" + fileName);
System.exit(0);
}
}
private String getRandomName( )
{
int index = randomInteger.nextInt(numberOfNames*2);
return studentNames[index];
}
private String getRandomID( )
{
int number = randomInteger.nextInt(100000000) + 100000000;
String value = Integer.valueOf(number).toString( );
return "s" + value.substring( 1, value.length( ));
}
private double getRandomGPA( )
{
int gpaInteger = randomInteger.nextInt(200) + 201;
double gpaDouble = gpaInteger/100.0;
return gpaDouble;
}
public Student getStudent( )
{
String name = getRandomName( );
String id = getRandomID( );
double gpa = getRandomGPA( );
return new Student(name, id, gpa);
}
}
//Student.java
http://m.uploadedit.com/busd/1615302385423.txt
//Node.java
http://m.uploadedit.com/busd/1615302534461.txt
//boynames.txt
http://m.uploadedit.com/busd/1615302576105.txt
//girlnames.txt
http://m.uploadedit.com/busd/1615302624457.txt
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "test.Node.setNext (test.Node)" because "temp" is null at Assign4b_new/test. SinglyLinkedlist.insert (SinglyLinkedlist.java:33 at Assign4b_new/test. StudentApp.main(StudentApp.java:64) You will need to change the path in RandomStudents Class for the boynames.txt and girlnames.txt to your directory of choice at lines 17,18 when you go to test the application.[
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
