This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes How can I use println to display the value of data when I node.setData((TreeNode) deviceCollection.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I use println to display the value of data when I node.setData((TreeNode) deviceCollection.." Watch "How can I use println to display the value of data when I node.setData((TreeNode) deviceCollection.." New topic
Author

How can I use println to display the value of data when I node.setData((TreeNode) deviceCollection..

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I have a class that has TreeNode data; with setters and getters. Im trying to bind values to data by using setData but I need to know how to see what the value is of what Im binding to data. How should I go about this?

The only way I know to do this as of right now is to go ahead and bind it to the tree it will eventually be used to display. But if I could check what is being bound to data before that it would take out an element of uncertainty


True wisdom is in knowing you know nothing - Socrates
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I think I have it figured out, if in my class that has the data variable I put a print method:



and then put a main method in another class that is manipulating the data through the setData method:



IF I am able to get the value of data to change with this class it should come out when I run this class right?


edit - nope, I changed it from a TreeNode to a String an dthen just did node.setData("value"); and it still printed out null - Im so sidetracked now, I need to get back to my original problem but I am stuck and cant seem to get it figured out
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

IF I am able to get the value of data to change with this class it should come out when I run this class right?

edit - nope, I changed it from a TreeNode to a String an dthen just did node.setData("value"); and it still printed out null - Im so sidetracked now, I need to get back to my original problem but I am stuck and cant seem to get it figured out


To be blunt, I have no idea what you are trying to say... Care to show us with an example?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Im sorry, I know it is unclear, dont worry about this one, i actually found a different way to test some of this. It turns out I am not even getting as far as that. Instead I am trying to print deviceColelction from this class:



But I get a null pointer exception. So it seems to me that deviceCollection is not getting the value that Im trying to set it to. But I really dont know where to start to fix this. Im pretty much just floundering here which is the assumption most regualrs to these forums have already come to Im sure lol
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

But I get a null pointer exception. So it seems to me that deviceCollection is not getting the value that Im trying to set it to. But I really dont know where to start to fix this.


You "start" with the NPE. The stacktrace should provide you with the filename and line number, along with the call stack, on how you got there. Don't assume. Confirm the null pointer exception.

Henry
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Hmm, OK. I didnt know this, thanks. Here is the full stack:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:80)
at org.simpleframework.xml.core.Persister.read(Persister.java:471)
at org.simpleframework.xml.core.Persister.read(Persister.java:452)
at deviceContainer.DeviceCollectionXMLAO.readXML(DeviceCollectionXMLAO.java:15)
at testContainer.Temp.updateNode(Temp.java:44)
at testContainer.Temp.main(Temp.java:25)

Im gonna do a search for some of that and see what I can get

If I change the code around a bit by removing both of the throws from the main and updateNode class, eclipse then tells me I need to add a ctach/throw declaration which i do and then I must initialize deviceColelction which I do to null. The code changes to this:



WHen I run that i get

java.lang.NullPointerException
printingnull
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:80)
at org.simpleframework.xml.core.Persister.read(Persister.java:471)
at org.simpleframework.xml.core.Persister.read(Persister.java:452)
at deviceContainer.DeviceCollectionXMLAO.readXML(DeviceCollectionXMLAO.java:15)
at testContainer.Temp.updateNode(Temp.java:46)
at testContainer.Temp.main(Temp.java:26)
Exception in thread "main" java.lang.NullPointerException
at testContainer.Temp.updateNode(Temp.java:57)
at testContainer.Temp.main(Temp.java:26)

so at least it is printing out the string I put in the println ("printing") but then after that it has null, so it seems the deviceCollection is staying null which is what i initialized it to. I think that also suggests that deviceColelction is just not being set to what I am trying to set it to

man I might just have to use a .properties file to poulate my tree nodes, and get this project running as far as I can in that manner. Then once I have all that complete, if there is still time at the end I can try to return to this problem. I just cant get a richfaces tree to populate from xml, it is so frustrating
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


Basically... The readXML() method has been called, which means that the method.getResponseBodyAsStream() call did return a value. Unfortunately, there is something wrong with the value. If you have the source to the simple framework, take a look at the Persister class, particularly line 471 -- you need to figure out what that line is accessing, and why the value that was passed in was invalid.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

so at least it is printing out the string I put in the println ("printing") but then after that it has null, so it seems the deviceCollection is staying null which is what i initialized it to. I think that also suggests that deviceColelction is just not being set to what I am trying to set it to


Of course deviceCollection is staying null. The assignment is failing. There is no value (returned from readXML()) to set it to. The exception aborts the assignment. You didn't need to make all those code changes to confirm that.

Henry
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I hate to ask but youve been more helpful than anyone so far in teaching me a very valuable skill - how to troubleshoot. I didnt even knwo i could look at this stuff,.
So maybe if you dont mind youd help me interpret what I got when I clicked on java:471 (and finally figured out what you meant by the source 0 I have no idea how I lucked out and interpreted what that meant but I did)

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


Just get comfortable with the stack trace. If you go up (the stack) by one, you'll see that it failed during the constuctor of the InputStreamReader() which means that it is either the second or third parameter of that method that is null.

To see how that method is called, go down the stack by one -- specifically, take a look a line 452 of the same file.

Henry

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Here is my whack at the problem lol..

So here is the readXML method:



I dont think there is a problem with this method because it was written by someone with a lot of experience.

So if Im correct it means devicesUrl is where the problem is.

static String devicesUrl = "http://webc-apps.ni.com/measure/1.01/devices";

when you go to that page all you see is:



So I *think* I am needing to add

<?xml version="1.0" encoding="UTF-8"?>

to the top of that.

Probably wrong, Im just getting in a guess before I go to bed. I have work at 6 am ><

I will study these things more though and see what else I can find out,

Thank you for your wisdom
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I dont think there is a problem with this method because it was written by someone with a lot of experience.

Heh.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

lol, constructive, thanks

I guess I shouldnt assume as much though
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Guys with experience *also* believe your signature quote.

Have you verified the InputStream isn't null?

(And the readXml() method makes me nervous, because it's closing the input stream, but where it's opened is anybody's guess--methods with side effects like that can be unnecessarily difficult to debug, and give me the heebie-jeebies. That's not really related to your issue, though.)
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

bah I cant even sleep because this problem keeps bothering me.

I dont really know how to check honestly, I will definitely look into it though and post back here once Ive figured it out.

Im thinking the input source is going to be from the line

deviceCollection = serializer.read(DeviceCollection.class, xml);

and right above that I do see

DeviceCollection deviceCollection = null;

but when I remove the = null I get a new line in the stack

at java.io.InputStreamReader.<init>(InputStreamReader.java:80)

which looks like:



I dont yet know what to make of that so I still need to do some research.

Thanks for the help, I will see what I can extract from all this
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Removing "= null" from "DeviceCollection deviceCollection = null;" shouldn't change program behavior: deviceCollection will be null anyway if it's not explicitly set.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I can probably use system.out.println(method.getResponseBodyAsStream()); to see what value is being passed to access.readXml right?
Man I wish I wasnt at work so I could work on this more
or wait would I need to use method.getResponseBodyAsString when printing out the value?

If you were in my shoes, what would you be looking into? I guess what would be the terms you search for? Im definitely willing to do the work, I just dont really have a starting point to go from
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I posted this as its own topic but I also want to put it here since they are really the same problem:

When I print out some values to see what they are I get:



The first line, responseBody is of the type InputStream
The second line deviceCollection of the type DeviceCollection, in teh deviceContainer - It is this line I am mainly concered with. It is a stream so I had no idea what to expect when I print it out, just as long as it wasnt null!! But I dont know what to make of printing deviceCollection: deviceContainer.DeviceCollection@1764be1 - does this mean I actually was able to bind the responseBody to deviceCollection as a stream? (through this line: deviceCollection = access.readXML(method.getResponseBodyAsStream(null));)

And the third line data is of the type TreeNode, but that line is pretty obvious



So Im unsure how to interpret this. The first line seems to suggest it is working correctly right, because it is calling an auto close method on the stream right? However when I change some things in that class and try to also print out responseBody as a String, I get:



I think this also might be OK though, because if Im right it is saying it received the data I wanted and printed it out as a string, but when it also tried to print it as the stream, the stream was already closed. I think this is what this means, please clear this up for me though. deviceCOllection form teh third line also changes to null when I do this, but I would guess that is also the result of the stream being auto closed right?

It is this second line that Im mainly concerned with. Im trying to bind the responseBody which is that xml to deviceCollection. It seems I was successful in that first bit up there right? Or am I misinterpreting?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I use println to display the value of data when I node.setData((TreeNode) deviceCollection..