Re: Convert String to Object - Newbie Question



On 2007-09-28, Marion <mchew02@xxxxxxxxxxx> wrote:
Hello all,

I am trying to convert a myString declared as a String[] and convert
it into an Object so that I can use the non static method.

Currently, my program does not compile. The error I'm receiving is my
old friend error msg: "non-static method setData....cannot be
referenced from a static content.

This error likely has nothing to do with converting a String[] into an
Object.

Essentially, I want to read in an array of Strings into objects for
data manipulation (a sort to be exact.).

The program looks something like this:
...
public void init (String[] array, int size){
.// assume array[0] is the first string read in from the text file
String dataString = array[0];
Node.setData(dataString);
...
}
Class Node {
(constructor stuff declared here)
}
...
public void setData(Object data){
this.data = data;
}

I read from the Java docs that String was an object (or was extended
by it) so doesn't that mean that String is an object? I'm beginning
to dislike objects! lol,

Not only is a String an Object, but a String[] is alos an object.

I know how to turn integers into objects with:
Integer one = new Integer(1);

why doesn't "String myString = new String (array[0]);:
work?

It works. Here is a small class that demonstrates that it works (as
long as you run the class with at least one argument value).

public class Test {
public static void main(String[] array) {
String myString = new String(array[0]);
System.out.println("myString = " + myString);
}
}

If you dislike objects, you should not try to write Java programs.

I tried to read documentation for java.io.Serializable (not sure if
that's what it's called but something like that) and on reflections.
being new to java, those documents I read scared the begeezus off of
me. (didn't understand em, nosiree)

Neither serialization nor reflection is involved here.

I tried to use "stringToObject(string,class)" statement and sadly, I
am having trouble understanding how to use it in my program. There
are no examples anywhere.

All I want is to convert a string to object and it has gotten suddenly
complicated. :(

Ok, well it's 4am here, I'll try this again in the morning. :)
Anyone with hints or suggestions, please, I'm open to them.

Getting back to the actual problem: "non-static method setData....cannot
be referenced from a static content.", the Java compile is complaining
about the line

Node.setData(dataString);

Because setData is a non-static method, you need to invoke the method
on a Node object (or an object that has Node as an ancestor class). In
the code that you have given there are no Node objects. In the
epression

Node.setData(dataString);

"Node" is the name of a class, not a reference to a Node object (an
instance of the class). Where it the Node object whose data you want to
set?
.



Relevant Pages

  • Re: passing value from jcombobox to other classes?
    ... public void setX(String x) { ... // the reference to an object of A ... Run it by "java C" and see what happens. ...
    (comp.lang.java.programmer)
  • LONG: Java student getting String Exception/String Index errors
    ... Hello, all, I'm back with more questions from my Java I class. ... and length' to edit a single long string of text. ... public void setTextFind{ ... errors indicate I'm using a number that's too large for substring ...
    (comp.lang.java.help)
  • Xml parser and character encoding
    ... I am new to java and I run a short program processing xml files. ... private String CData; ... public void startElement(String namespaceURI, String localName, String ...
    (comp.lang.java.programmer)
  • Re: A class that uses instances of itself? Is this right?
    ... > I was recently sent some Java code that I find VERY odd. ... > public String getText() { ... 'TypeSafe enum' pattern. ... public void aMethodDoingSomethingOnEnum{ ...
    (comp.lang.java.programmer)
  • CLASSPATH question
    ... If I have *.JAR files in the same directory that I am compiling my java code ... public void setDocumentLocator(Locator locator) ... public void startElement(String namespace, String name, String qName, ...
    (comp.lang.java.help)