0

Whenever I try to run this code:

import java.util.Scanner;

public class KCFRodriguez2019101507 {

        Scanner sc = new Scanner(System.in);
        static String empName, position, status;
        static int empNo, salaryReg, salaryCon, year, deduction, netPay;
        static float deduct;
        
    @SuppressWarnings("null")
    public static void main(String[] args) {
        @SuppressWarnings({ "unused", "resource" })
        Scanner sc = new Scanner(System.in);
            String a = "Employee Name:";
            String b = "Status:";
            String c = "Position Title:";
            String d = "Employee Number:";
            String e = "Regular Salary:";
            String f = "Contractual Salary:";
            String g = "Year Hired";
            
            System.out.println(a);
            Scanner entry = null;
            empName = entry.nextLine();
            System.out.println(b);
            status = entry.nextLine();
            System.out.println(c);
            position = entry.nextLine();
            System.out.println(d);
            empNo = entry.nextInt();
            System.out.println(e);
            salaryReg = entry.nextInt();
            System.out.println(f);
            salaryCon = entry.nextInt();
            System.out.println(g);
            year = entry.nextInt();
            
    }
}

I get an error saying that "exception in thread 'main'", so I cant compile the code. I'm fairly new to java, so I don't know how to fix this. I'm trying to make a program that allows the user to enter Employee data.

Error Code: Employee Name: Exception in thread "main" java.lang.NullPointerException at KCFRodriguez2019101507.main(KCFRodriguez2019101507.java:24)

kianclark
  • 11
  • 1
  • Can you please give the whole error output? It should say more. – Higigig Jul 16 '20 at 23:10
  • Employee Name: Exception in thread "main" java.lang.NullPointerException at KCFRodriguez2019101507.main(KCFRodriguez2019101507.java:24) – kianclark Jul 16 '20 at 23:12
  • Can you please accept the answer if you believe it is correct? Welcome to Stackoverflow and have fun! – dbaltor Jul 16 '20 at 23:37

1 Answers1

0

You're making entry the value null, and in Java you can't use null to access data. Change this line:

Scanner entry = null;

To this:

Scanner entry = new Scanner(System.in);

By the way, you define the sc variable. Delete this line:

Scanner sc = new Scanner(System.in);
Higigig
  • 907
  • 1
  • 3
  • 15