Avatar of krakatoa
krakatoa
Flag 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

Java

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
Jeffrey Dake

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.
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.
CEHJ

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
krakatoa

ASKER
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

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?
krakatoa

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jeffrey Dake

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.
CEHJ

 at Mistral$FileSender.sendFiles(Mistral.java:638)

Open in new window

Which is line 638 of the last code you posted?
CEHJ

Line 638 is one one in the catch block here :
I missed that.

That's not possible. Do you mean the try block?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CEHJ

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
krakatoa

ASKER
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
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
krakatoa

ASKER
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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
krakatoa

ASKER
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.
krakatoa

ASKER
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.
CEHJ

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
krakatoa

ASKER
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. ;)
CEHJ

:)
krakatoa

ASKER
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. ;)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

Yes. Of course, the principal point of generics is the provision of type-safety, so this of course means casting isn't necessary
krakatoa

ASKER
Yes.

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