Results 1 to 10 of 10
  1. #1
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default Null Pointer Exception via setter method?

    Here are the snippets of my code, whenever it gets to the setter method call in the method readEmployee, I get a null pointer exception.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ReportFormatter {
    	
    	public static void main(String [ ] args) throws FileNotFoundException, UnsupportedEncodingException, IOException{
    		Employee[] employee = new Employee[1];
    		Department[] dept = new Department[4];
    		
    		Employee.readEmployee(employee[0]);
    		System.out.println(employee[0].getEmployeeId());	
    		
    	}
    	
    }
    And within the employee class is readEmployee. ( I know this code is inefficient, I tried using it
    as a non static method to apply its values, but I got the null pointer exception there as well).

    Java Code:
    public static void readEmployee(Employee emp) throws FileNotFoundException, UnsupportedEncodingException, IOException{
    				FileInputStream input = new FileInputStream("E:\\Employee.dat");
    				DataInputStream data = new DataInputStream(input);
    				InputStreamReader reader = new InputStreamReader(input,"US-ASCII");
    				 String employeeId = "";
    				 int employeeIndicator;
    				 int departmentCode;
    				 String firstName;
    				 String lastName;
    				 float salary;
    				 int hireMonth;
    				 int hireDay;
    				 int hireYear;
    				 int vacationDays;
    				 String hireDate;
    				employeeId += (char)reader.read();
    				employeeId += (char)reader.read();
    				employeeId += (char)reader.read();
    				emp.setEmployeeId(employeeId);
    				reader.close();
    				data.close();
    				input.close();
    			}

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) You declare arrays that don't have any values (not initialized) => The arrays contain nulls. Then you call a method passing the first element of your array to it (null).
    2.) Your method then does some reed dance with some streams and later calls emp.setEmployeeId(employeeId); But emp passed in was null so in effect you have done null.set... which throws a N.P.E.
    3.) Don't use those classes for reading character files. Use FileReader/BufferedReader combination or Scanner class instead. Those streams are for reading binary data files.

  3. #3
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    I am reading a binary file. Some of it is ASCII, therefore I must convert it from ASCII into Unicode. Its a binary file containing employee information. The method is not done yet, I just tested it with one of the values being set (eventually it will be all of the values).

    So must I initialize all parameters/values the Employee objects? This is not a problem, albeit a hassle. Is there anyway I could have the constructor do this? Can you have if statements/loops/filestreams within the constructor?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you mean by "Some of it is ASCII"? It's a mixture of ASCII and what?
    As for initializing all the values, initialize to what?
    If you have a file containing Employee information and your goal is to read the information into a collection of Employee objects, then you need to

    1.) Initialize an ArrayList<Employee>
    2.) Go through the file reading the data, constructing a new Employee using the read in data and
    3.) Adding that newly created Employee into the list of employees.

    Notice that I have made assumptions about your goal. It would have been better if you had stated that yourself.

  5. #5
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    What do you mean by "Some of it is ASCII"? It's a mixture of ASCII and what?
    As for initializing all the values, initialize to what?
    If you have a file containing Employee information and your goal is to read the information into a collection of Employee objects, then you need to

    1.) Initialize an ArrayList<Employee>
    2.) Go through the file reading the data, constructing a new Employee using the read in data and
    3.) Adding that newly created Employee into the list of employees.

    Notice that I have made assumptions about your goal. It would have been better if you had stated that yourself.
    Indeed, I meant no offense. I try to keep only the necessarity information in order to solve the problem (ie, get past the null pointer exception). The employee.dat(binary) file I am reading consists of this.. (copied/paste from my specs).

    Employee ID 3 bytes ASCII
    Employee/Contractor Indicator 1 byte ASCII E for employee, C for contractor
    Department Code 1 byte ASCII
    Last Name 20 bytes ASCII, blank padded
    First Name 15 bytes ASCII, blank padded
    Salary float single precision
    Hire Date 1 byte binary identifies the month, 1 byte binary identifies the day, 2 bytes binary identifies the year
    Vacation Days 2 bytes signed binary
    Training 1 byte

    Do I really need a ArrayList instead of an array? I have no experience with those compared to just a regular array.

    After you pointed out my flaw in my Employee array containing "null" values, I was just planning on initializing all values to 0, then using setters to set values for each variable as I read them in.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Are you sure you are not confusing bytes with characters?
    From your description you probably just have a fixed length format character file that you need to read line by line. At each read you assign the contents of the line to String variable and use the substring method to access specific parts that you need to use for creating an Employee. Just use the approach I've suggested above.
    You only use an array if you know in advance exactly how many records you are going to read.

  7. #7
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    h ttp://dl.getdropbox.com/u/1803403/Employee.dat

    (take the space out of the h and the t.)

    This is my Employee.dat file. I'm almost positive it is total binary. I opened it up in a binary viewer and the order/layout as posted is what it is in the file (unless im reading it wrong). My professor had the last name and first name in the wrong order though, so i fixed it.

    I have the one stream (data) and the one reader (inputStreamReader) "hooked" onto the inputstream. I'm hoping that I will be able to use the reader for the ASCII when needed, then the data stream for the float/other values.

    Yeah, you are right, I do not have the exact amount of employee's that are located in the .dat file. However, what I was planning on doing, is that by initializing all employee's in the array to have values of 0's, I was going to read the .dat file until it recieved a -1 (stating it had reached end of the file), and then I will print all Employee "records" that do not have a value of 0 for the employeeId.

    I do realize how.. odd this might be, but my professor did not give me the number of employee's stored.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can't initialize Employees to 0. They are not numbers. Besides Java already initializes the elements to null. The fact that you don't know how many elements you want to read means that you need to use an ArrayList. You seem to be quite lost in this exercise and to make matters worse, you also seem to failing to follow what I'm posting. Follow the steps I have suggested and post if you get stuck with that.

  9. #9
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Thank you for your help thus far. I meant setting the parameters of the Employees to 0/"0", and then overwriting them whenever I read the file. However, I will go my own way with this for right now. Thanks for your help.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by zerkz View Post
    .. However, I will go my own way with this for right now. ...
    Erm, good luck.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  3. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •