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

LVL 17
krakatoaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey Dake Senior Director of TechnologyCommented:
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.
krakatoaAuthor Commented:
But Jeff, all the files I pass in provide a valid name as you see in the code, those names all get printed.
CEHJCommented:
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

krakatoaAuthor Commented:
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

Jeffrey Dake Senior Director of TechnologyCommented:
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?
krakatoaAuthor Commented:
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

Jeffrey Dake Senior Director of TechnologyCommented:
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.
CEHJCommented:
 at Mistral$FileSender.sendFiles(Mistral.java:638)

Open in new window

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

That's not possible. Do you mean the try block?
CEHJCommented:
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
krakatoaAuthor Commented:
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?
CEHJCommented:
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?
No. Your (self-misleading) debug output "vec is false" (which should be more like "(vec == null) is false") should not be both "TRUE" (per your comment) AND false, so i'm wondering which it is. I'm assuming you're being told vec is NOT null

vec = new Vector<String>(fArr.length);

Open in new window

should be if possible changed to
Vector<String> vec = new Vector<String>(fArr.length);

Open in new window

to insulate that from error.

If my above assumption is correct, you might find "oos" is what is null.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaAuthor Commented:
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?
krakatoaAuthor Commented:
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.
krakatoaAuthor Commented:
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.
CEHJCommented:
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
krakatoaAuthor Commented:
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. ;)
CEHJCommented:
:)
krakatoaAuthor Commented:
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. ;)
CEHJCommented:
Yes. Of course, the principal point of generics is the provision of type-safety, so this of course means casting isn't necessary
krakatoaAuthor Commented:
Yes.

I guess you read my earlier about the sslsocket rôle ? Groan. Another grind.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.