• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Java simple question

Status
Not open for further replies.
Level 19
Joined
Jul 2, 2011
Messages
2,162
Hello

I am busy making an error report for all my programs and I want the program to print out the values of all the components along with their names.

However.

Components do not have names by default, resulting in null unless I code in the name which would be perfectly useless.

My question is, how does java identify components if it doesn't name them? If I could get an ID even if it is very obscure, I could at least use that information to search for the values of components I'm looking for.

Some of my components have over 80 different components or generate even more, naming them in code isn't really an option.

///

code:

for(int ko=0;ko<GameBack.getComponentCount();ko++){

System.out.println(GameBack.getComponent(ko).getName);
//result null
}

//however if I set name
Box1.setName("Box1");

//result Box1

if Java doesn't see the component as Box1 what does it see it as?????

//////////////////////////////////////////////////////////////////
Solved!
 
Last edited:
Level 19
Joined
Jul 2, 2011
Messages
2,162
Here's the simplest working example, no need to do any manual setName: Ideone.com

If you get null, then your class is null, and thus your objects are probably not initialized, but like I said, can't really come to any concrete conclusion without seeing code.
The result is it just prints out what the component is, 'javax.swing.JButton'

I'm looking for the actually components name, like if I name it button1

However it seems it is impossible to get such variables, :(
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
You can't automatically get the name of a variable, because it's not deterministic.
Let's say you call it button1, and you then declare a second variable called button2 and set it to button1, what is its name?
What you can do instead is use a map instead of an array, and then you have also the keys. Of course, that's logically quite the same as calling setName() manually, or any other kind of identifier, so it's really a matter of style at this point.
 
Last edited:
Level 19
Joined
Jul 2, 2011
Messages
2,162
You can't automatically get the name of a variable, because it's not deterministic.
Let's say you call it button1, and you then declare a second variable called button2 and set it to button1, what is its name?
What you can do instead is use a map instead of an array, and then you have also the keys. Of course, that's logically quite the same as calling setName() manually, or any other kind of identifier, so it's really a matter of style at this point.
You can't automatically get the name of a variable, because it's not deterministic.
Let's say you call it button1, and you then declare a second variable called button2 and set it to button1, what is its name?
What you can do instead is use a map instead of an array, and then you have also the keys. Of course, that's logically quite the same as calling setName() manually, or any other kind of identifier, so it's really a matter of style at this point.
I printed out the line in which it is createdd in code instead.

that way it is an address which I can easily find.

thanks for the help
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
You can't automatically get the name of a variable, because it's not deterministic.
Let's say you call it button1, and you then declare a second variable called button2 and set it to button1, what is its name?
What you can do instead is use a map instead of an array, and then you have also the keys. Of course, that's logically quite the same as calling setName() manually, or any other kind of identifier, so it's really a matter of style at this point.
You can't automatically get the name of a variable, because it's not deterministic.
Let's say you call it button1, and you then declare a second variable called button2 and set it to button1, what is its name?
What you can do instead is use a map instead of an array, and then you have also the keys. Of course, that's logically quite the same as calling setName() manually, or any other kind of identifier, so it's really a matter of style at this point.
I printed out the line in which it is createdd in code instead.

that way it is an address which I can easily find.

thanks for the help
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,192
One can use java.lang and its many sub packages, eg java.lang.reflect, to dynamically lookup all the methods and members of a class object, even the private ones. Be aware that accessing private entities will need sufficient runtime permission otherwise a security exception will be thrown. This is useful for accessing the null argument constructor, which is always present for all objects even if masked by other constructors, such as is done by the ObjectInputStream implementation for standard java Serializable.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
One can use java.lang and its many sub packages, eg java.lang.reflect, to dynamically lookup all the methods and members of a class object, even the private ones. Be aware that accessing private entities will need sufficient runtime permission otherwise a security exception will be thrown. This is useful for accessing the null argument constructor, which is always present for all objects even if masked by other constructors, such as is done by the ObjectInputStream implementation for standard java Serializable.
I'm not sure about all that since it sounds buggy, but you have inspired another possibility

system.err seems to know the names of components. meaning I could write down the value of the component, then give it a null value and system.err will provide the name of the damaged component :D

Perfect thanks bro!
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
Can you show actual code and the results you are trying to obtain? what you are trying to do is the most hack-y unreliable thing I've ever heard of.
I'm going to try and code the new addition now, but I have a screwed up version to show if you are interested?

Here is a print out of the result,

Usersubmitted_



___Component___java.awt.TextArea[text0,10,10,1248x646,text=,editable,selection=0-0,rows=0,columns=0,scrollbarVisibility=both]


___Component___javax.swing.JButton[,10,666,55x31,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@159cd303,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Play,defaultCapable=true]


___Component___javax.swing.JRadioButton[,77,672,66x19,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@b6c72a8,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Normal]


___Component___javax.swing.JRadioButton[,161,672,50x19,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@28be4414,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Fast]


___Component___javax.swing.JRadioButton[,229,672,63x19,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@3bed753b,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Faster]

....

as you can see right now there is no given name for what the component's name is in the code, but it does give you the value of the component. I will work on getting the name now

_
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,192
Yes but you can put that out put stream into a text file, then convert that text back into a String in the program and from there filter and obtain the name alone
...

You could also encrypt that file, copy it a thousand times, delete all but the last copy and then decrypt that file while at it. All completely pointless though like writing it to standard error in the first place...
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
As in show your class/component container/whatever.
How do components have names if you store them in an array or some other structure that doesn't use names as indices?
Well I guess it is no surprise that it is impossible to get the name of the components from the java.

Your question is the exact same question I was asking. How does java know what the component I'm calling is if it has no name? I have a name in the code, sure, but name can not be retrieved from java why?
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
And yet you are as usual not showing code, while being completely unclear as to what you want to do. What on earth is "the name of the components"? Code.
K I'll produce the code

JavaScript:
        int reset = 0;//determines if the user has activated the error report or the game has on start up(which all games should do to direct all error reports to text)
        if(this.isShowing()==false){reset=1;}//if this game hasn't initialised Error reports will be ordered to write into a Text
        String[] Games = {"EinsteinPuzzle", "Equations", "Memory", "EverthingMaths", "SimplyMath", "SpiderMath", "makeAword", "CarryOnMath"};
        PrintStream out=null;
        try {

            out = new PrintStream(new FileOutputStream("Temp_" + Games[Data[3]] + ".txt",true));
            System.setErr(out);//Writes errors to Temp_txt the file created above
            System.err.println("_Error Log_");//pastes a heading over all errors, if no errors you just get a consecutive collection of _Error Log_

        } catch (FileNotFoundException ex) {
            //if no such file is found, one will be created and this trigger fired again
            File Myfirst = new File("Temp_" +Games[Data[3]] + ".txt");
            try {
                Myfirst.createNewFile();
                ErrorReportMouseClicked(null);
            } catch (IOException exp) {
                Logger.getLogger(this.getName()).log(Level.SEVERE, null, exp);
            }
        } catch (IOException ex) {
        }
        while (reset == 0) {

            System.err.println("_");//writes the error into the log
            System.err.println();
            System.err.println("Usersubmitted_");
            System.err.println();//writes the error into the log
            for(int ko=0;ko<GameBack.getComponentCount();ko++){
                try {

                    System.err.println();
                    System.err.println("\n___Component___"+GameBack.getComponent(ko).toString());//Submits the contnent of each Component so that I can see if something doesn't belong

                } catch (SecurityException ex) {
                    Logger.getLogger(this.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.err.println();
            System.err.println("_");//writes the error into the log
            out.close();//closes out

            reset++;//stops this loop
            JOptionPane.showMessageDialog(null,"Thank you for reporting this error,\n"+Games[Data[3]]+" will now shut down and return you to WindSong");

            setEnabled(false);
            new WindSong(Index, scoreCard).setVisible(true);
            dispose();
        }
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
As in show your class/component container/whatever.
How do components have names if you store them in an array or some other structure that doesn't use names as indices?
You can give a component a name but java will not recognise it until you say, *ComponentName*.setName("bla");, until that point java does not recognise the name you use to code that component.

Meaning despite the fact that you use the name you gave the component in order to write into it and what ever else, java does not use that name.

Creating a component.

JButton Bone=new JButton();

Does not mean that the JButton of Bone has the name Bone, it is only when you say, Bone.setName("Bone"); that that component retains the name Bone

This is the problem I have with my error reports, how do I get the name and values of components? Right now I can get the value of all the components but not their names
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Do you know what "the name of an object" is? it's nothing. Objects don't have names. Variables have names. You can't get the names of variables at run time.

There is no problem here. Objects don't have names. Objects have types (classes in Java). Objects in Java have members, and methods. Objects DO NOT HAVE NAMES.

If you want to add your own metadata, you are free to do so in any of the many ways (like using setName, subclassing and adding your own member, using a map, or what not).

Again, for the third time, I don't care about your logging code. The component container. How do you initialize it. How do you add components. If you are storing the components in an array, and want them to magically store a name, then you are doing it wrong.

If you want to print their values, that's a totally different question, and it's called serialization/marshaling.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,192
as you can see right now there is no given name for what the component's name is in the code, but it does give you the value of the component. I will work on getting the name now
Why do you even need the variable/member name? As GhostWolf has said (which did not load for me until after this post), that is a variable name and has nothing to do with the object. It is not even a named storage location for an object like one can make in C/C++, just a storage location for an object handle.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
Why do you even need the variable/member name? As GhostWolf has said (which did not load for me until after this post), that is a variable name and has nothing to do with the object. It is not even a named storage location for an object like one can make in C/C++, just a storage location for an object handle.
I need it because when a user submits an error report, I want an automatic print out of all the values so that I can see if any of them have incorrect content.

and before you shout at me too, I understand that these components do not have names according to java. what I want to know is how do I find out which values belong to which components? right now I can get all the values of the components but not the components names them selves? that's crazy!

there must be a way to know which components I am getting the values from

...

Do you know what "the name of an object" is? it's nothing. Objects don't have names. Variables have names. You can't get the names of variables at run time.

There is no problem here. Objects don't have names. Objects have types (classes in Java). Objects in Java have members, and methods. Objects DO NOT HAVE NAMES.

If you want to add your own metadata, you are free to do so in any of the many ways (like using setName, subclassing and adding your own member, using a map, or what not).

Again, for the third time, I don't care about your logging code. The component container. How do you initialize it. How do you add components. If you are storing the components in an array, and want them to magically store a name, then you are doing it wrong.

If you want to print their values, that's a totally different question, and it's called serialization/marshaling.
I know their values, but I need to know their names from which the values belong.

example. I know the value of button1, 2, 3, 4 but java won't tell me which button these values belong to
 
Last edited:

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,192
I need it because when a user submits an error report, I want an automatic print out of all the values so that I can see if any of them have incorrect content.
In which case see my previous answer. Use the lang and lang.reflect packages on the class to iterate through all members of a object and print them out. Private members may require elevated security to access.

Specifically get the Object, get the object's Class, get the Field s of that class and then for each Field get its name and the value of the field from the Object. Be warned that this might expose internal mechanics of a class to the user.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
In which case see my previous answer. Use the lang and lang.reflect packages on the class to iterate through all members of a object and print them out. Private members may require elevated security to access.

Specifically get the Object, get the object's Class, get the Field s of that class and then for each Field get its name and the value of the field from the Object. Be warned that this might expose internal mechanics of a class to the user.
this would only result in the print out I currently already have.

giving me the value coordinates and a whole lot of other information but not the components name/Not button1 showing where this values come from.

meaning though I get all the content of the buttons and such on screen, I do not get the any information telling me where or who these values come from
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Code:
Printing out User12345 components:
   Component 0:
       Class: java.awt.Button
       Title: hello
       X: 500
       Y: 400
   Component 1:
       Class: java.awt.Whatever
       X: 800
       Y: 200
       Z: 10

And then you go to your code, and oh, component 0 is this thing, and component 1 is that thing.

Or, if you are really tricky, you simply have a map for debugging purposes, which you index when logging:
Code:
0: Button1
1: Something
2: SomethingElse


If you have dynamic components per user, then yes, you might want to give them identifiers, in which case setName() can indeed work well (or again, any other name<->object mapping).
Java:
ComponentClass component = ...;
component.setName("Button1");

container.addComponent(component);

// or if you want it shorter, add it to the method
container.addComponent(component, "Button1");

// yey one liner
container.addComponent(new MyComponent(bla, blabla), "Whatever");
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
Code:
Printing out User12345 components:
   Component 0:
       Class: java.awt.Button
       Title: hello
       X: 500
       Y: 400
   Component 1:
       Class: java.awt.Whatever
       X: 800
       Y: 200
       Z: 10

And then you go to your code, and oh, component 0 is this thing, and component 1 is that thing.

Or, if you are really tricky, you simply have a map for debugging purposes, which you index when logging:
Code:
0: Button1
1: Something
2: SomethingElse


If you have dynamic components per user, then yes, you might want to give them identifiers, in which case setName() can indeed work well (or again, any other name<->object mapping).
Java:
ComponentClass component = ...;
component.setName("Button1");

container.addComponent(component);

// or if you want it shorter, add it to the method
container.addComponent(component, "Button1");

// yey one liner
container.addComponent(new MyComponent(bla, blabla), "Whatever");
Thanks, you give me further reason to close this thread

This is exactly what I have come to the conclusion of, if you look above you will see I have the exact same print out values and in this case setting names simply will not help me.

Thank you for the help regardless
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,192
giving me the value coordinates and a whole lot of other information but not the components name/Not button1 showing where this values come from.
It will give you the button name if used on the container.

I think the problem here is that you have nameless "components" inside a list and somehow want a name from them. You will need to give the components a name field, or use a map.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
It will give you the button name if used on the container.

I think the problem here is that you have nameless "components" inside a list and somehow want a name from them. You will need to give the components a name field, or use a map.
I'm currently using a map and I am happy with that solution alone.
Thank you for all your help
Until next time, cheers :)
 
Status
Not open for further replies.
Top