We help IT Professionals succeed at work.

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

krakatoa
krakatoa asked
on
102 Views
Last Modified: 2019-02-23
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

Comment
Watch Question

Jeffrey Dake Senior Director of Technology
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT

Author

Commented:
But Jeff, all the files I pass in provide a valid name as you see in the code, those names all get printed.
CERTIFIED EXPERT
Top Expert 2016

Commented:
CERTIFIED EXPERT

Author

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 Technology
CERTIFIED EXPERT

Commented:
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?
CERTIFIED EXPERT

Author

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 Technology
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

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

Open in new window

Which is line 638 of the last code you posted?
CERTIFIED EXPERT
Top Expert 2016

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

That's not possible. Do you mean the try block?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
CERTIFIED EXPERT

Author

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?
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

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?
CERTIFIED EXPERT

Author

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.
CERTIFIED EXPERT

Author

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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
CERTIFIED EXPERT

Author

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. ;)
CERTIFIED EXPERT
Top Expert 2016

Commented:
:)
CERTIFIED EXPERT

Author

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. ;)
CERTIFIED EXPERT
Top Expert 2016

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

Author

Commented:
Yes.

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.