8 Replies - 220 Views - Last Post: 28 May 2019 - 03:53 AM Rate Topic: **--- 3 Votes

#1 lolxtremo32   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 19

null Pointer reading a Linked List

Posted 27 May 2019 - 11:12 AM

Hi, I am trying to read a Linked List from a binary file, but the program never enters in the try always goes to the finally and so it give me null Pointer. Any idea why?

public LinkedList leerLinkedList(){
        
    try (ObjectInputStream o = new ObjectInputStream(new FileInputStream(fileName))){
       LinkedList re =(LinkedList)o.readObject();
       return re;
    }
        
    catch (EOFException e){
           
    }
    catch (FileNotFoundException e){
        System.out.println("");
    }
    catch (IOException e){
        System.out.println("");
    }   
    catch (ClassNotFoundException ex) {
        System.out.println("");
    }
    finally{
        LinkedList lista =new LinkedList();
        lista.add(new Usuarios("Hola","esta dando fallo en algun lugar",4,4));
        return lista;
    }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: null Pointer reading a Linked List

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: null Pointer reading a Linked List

Posted 27 May 2019 - 11:20 AM

Your 'try' is wrong.

03	    try (ObjectInputStream o = new ObjectInputStream(new FileInputStream(fileName))){


That line should be inside the try's braces { }.

https://docs.oracle....ions/catch.html
https://www.w3school...a_try_catch.asp
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12617
  • View blog
  • Posts: 45,777
  • Joined: 27-December 08

Re: null Pointer reading a Linked List

Posted 27 May 2019 - 11:34 AM

I tend not to be in favor of serialization as a design principle. The files created with serialization are only useful for any application that you write. You can't take the file and immediately feed it to a database, for instance, as you could with a CSV file or similar. Similarly, a client may want to open the file in an Excel spreadsheet and browse the users stored in the file. It is not possible for the client to do so, if you have created the file using serialization.

As you make design decisions, it is always worthwhile to consider their impacts.
Was This Post Helpful? 0
  • +
  • -

#4 lolxtremo32   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 19

Re: null Pointer reading a Linked List

Posted 27 May 2019 - 11:48 AM

View Postmodi123_1, on 27 May 2019 - 11:20 AM, said:

Your 'try' is wrong.

03	    try (ObjectInputStream o = new ObjectInputStream(new FileInputStream(fileName))){


That line should be inside the try's braces { }.


Do you mean like this?

try {
        
       FileInputStream e = new FileInputStream(fileName);
       ObjectInputStream o = new ObjectInputStream(e);
       
       LinkedList re =(LinkedList)o.readObject();
       
       e.close();
       o.close();
       
       return re;
    }



View Postmacosxnerd101, on 27 May 2019 - 11:34 AM, said:

I tend not to be in favor of serialization as a design principle. The files created with serialization are only useful for any application that you write. You can't take the file and immediately feed it to a database, for instance, as you could with a CSV file or similar. Similarly, a client may want to open the file in an Excel spreadsheet and browse the users stored in the file. It is not possible for the client to do so, if you have created the file using serialization.

As you make design decisions, it is always worthwhile to consider their impacts.


I now it is not the most optimal thing to do but is only a school proyect, I´m still learning :bigsmile:/>
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: null Pointer reading a Linked List

Posted 27 May 2019 - 12:13 PM

A suggestion: Make sure all the catch blocks print something that reports what happened. A blank line is almost useless.
Was This Post Helpful? 0
  • +
  • -

#6 lolxtremo32   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 19

Re: null Pointer reading a Linked List

Posted 27 May 2019 - 12:27 PM

View PostNormR, on 27 May 2019 - 12:13 PM, said:

A suggestion: Make sure all the catch blocks print something that reports what happened. A blank line is almost useless.


They have usefull things, but they are written in Spanish so i didnt want to bother you.
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: null Pointer reading a Linked List

Posted 27 May 2019 - 12:43 PM

What variable has the null value?
I do not get a NPE when I compile and execute the code.

This post has been edited by NormR: 27 May 2019 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#8 lolxtremo32   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-May 19

Re: null Pointer reading a Linked List

Posted 28 May 2019 - 02:19 AM

View PostNormR, on 27 May 2019 - 12:43 PM, said:

What variable has the null value?
I do not get a NPE when I compile and execute the code.


It doesn´t give you null pointer because it returns a Linked List in the finall. But the program never execute the try code.
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,836
  • Joined: 25-December 13

Re: null Pointer reading a Linked List

Posted 28 May 2019 - 03:53 AM

Quote

it returns a Linked List in the finally

Yes, that is what code in a finally does. It is always executed.

From the API doc https://docs.oracle....ns/finally.html

Quote

The finally Block
The finally block always executes when the try block exits


Quote

the program never execute the try code.

How do you know that? Add some print statements to the try code to show if it executes.

This post has been edited by NormR: 28 May 2019 - 03:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1