Link to home
Start Free TrialLog in
Avatar of krakatoa
krakatoaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How is this variable (vec) suddenly not being seen? (NPE). The code has not been altered since the calls last functioned properly.

vec = new Vector<String>(fArr.length); // Vector<String> vec declared earlier.
    
                        
            for(File fi : fArr){vec.add(fi.getName());}
            
            for(Object s : vec.toArray()){System.out.println((String)s);} // this prints the filenames to the console
            
            System.out.println("Files length is  "+fArr.length);// prints whatever length the array is
            System.out.print("vec is "); 
            System.out.println(vec==null); // prints TRUE !!

Open in new window

Avatar of Jeffrey Dake
Jeffrey Dake
Flag of United States of America image

If what you are passing in fArr contains a file that is returing null for its filename, you could get a null pointer exception.  Must likely that is what has changed, the input that is being passed in.
Avatar of krakatoa

ASKER

But Jeff, all the files I pass in provide a valid name as you see in the code, those names all get printed.
Output to the console  as follows :

Files length is  4
vec is false
java.lang.NullPointerException
        at Mistral$FileSender.sendFiles(Mistral.java:638)
        at Mistral$FileSender.access$000(Mistral.java:595)
        at Mistral.actionPerformed(Mistral.java:182)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

------------------------

Line 638 is one one in the catch block here :

try{
                  if(!(vec == null)){oos.writeObject(vec);}                               //send the filenames
             }catch(IOException oosexception){oosexception.printStackTrace();}

Open in new window

Based on what you pasted in it seems like the null pointer is coming after the block of code that you posted in the question.  Is there more code executed after that we are missing?
after the block of code that you posted . . .

Well yes it is, but it's literally the next statement after the first code . . . there's no call to anything or anywhere else. Here's how it looks in situ :

private void sendFiles(File[] fArr){
            
                        
            vec = new Vector<String>(fArr.length);
    
                        
            for(File fi : fArr){vec.add(fi.getName());}
            
            for(Object s : vec.toArray()){System.out.println((String)s);} // this prints out the names as they should be - no glitches.
            
            
            
            System.out.println("Files length is  "+fArr.length);
            System.out.print("vec is ");
            System.out.println(vec==null);/*
            System.out.print(" oos = ");
            System.out.println(oos==null);
            */
            
                        
             try{
                  if(!(vec == null)){oos.writeObject(vec);}                               //send the filenames
             }catch(IOException oosexception){oosexception.printStackTrace();}

Open in new window

Looking at the exception it looks like it is coming from a java swing library.  The block of code you are sharing doesn't appear to be using any.  Is there code that is running the interface that is calling this function?  There might be something going on there instead.
 at Mistral$FileSender.sendFiles(Mistral.java:638)

Open in new window

Which is line 638 of the last code you posted?
Line 638 is one one in the catch block here :
I missed that.

That's not possible. Do you mean the try block?
These are contradictory:
System.out.println(vec==null); // prints TRUE !!

Open in new window


Output to the console  as follows :

Files length is  4
vec is false
Yes, I meant thr try block.
I made a typo earlier too - so to be clear, line 638 is the issue- where oos (ObjectOutputStream), writes vec.

When, CEHJ, you say contradictory, I assume you mean that vec can’t be null if the filenames it contains get printed - is that what you mean?

Yes, the code is called from elsewhere - a swing context, whereby a FileChooser Open Files button sends the selected file(s) to this method. As I said, the exercise worked faultlessly until now, and I haven’t changed any of the codr. All I did change were some access qualifiers, but since both oos and vec can be seen in the method scope, how can line 638 complain NPE on vec?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'v really shot myself in the foot here - schoolboy error - yes, of course you're right, my simplistic hasty 'debug' is returning the polar opposite boolean. Doh.

So, back in reality . . . your declaration suggestion <<Vector<String> vec = new Vector<String>(fArr.length);>> - I've had that too, and I'll change it anyway, but it made no difference in terms of this particular side of the issue.

So it looks like oos must be null. That's odd if true, as it hasn't been touched since being written. Could the access modifiers' changes be at fault?
Oh I think I know what 'must' be happening - the SSL handshake isn't being done correctly and the oos is derived from what should be the ssl socket. So I think I have to redo the params.
The synergy did it; the synergy did it. Thanks to you both.

Still got to get it working fully, but btls this points to the direction.
for(Object s : vec.toArray()){System.out.println((String)s);}

Open in new window


can be just

for(String s : vec) {System.out.println(s);}

Open in new window


. your declaration suggestion <<Vector<String> vec = new Vector<String>(fArr.length);>> - I've had that too, and I'll change it anyway, but it made no difference in terms of this particular side of the issue.
No, i didn't think it would necessarily. But you should code defensively
Ok. Strange though - I thought I had that code you've posted as my first port of call, and it failed until it was cast. Perhaps my concentration dipped again, because it would be much better than the cast obviously. Thanks again anyway. ;)
:)
Yes, the no cast worked of course. I suppose my superannuation means partly that 'I only trust myself' when it came to dealing with the returned Object[], which I knew to be one of String. Generics, y'know, they can addle one's formerly fixed notions. ;)
Yes. Of course, the principal point of generics is the provision of type-safety, so this of course means casting isn't necessary
Yes.

I guess you read my earlier about the sslsocket rôle ? Groan. Another grind.