Simple null reference bug thats killing me (Java) - java

Alright so I am messing around with some code in java and I am getting a wierd error. I have my class Chaos which has a Window variable FSW, public as well. Now I have another class called Look. Chaos creates a Look and then runs the Look.Init() method. That init method runs the looks run method which tries to reference the FSW variable of its parent Chaos.
The problem is that no matter how I got about it whenever I reference -any- variable from Chaos from within Look the variable is null =/. I can call Chaos methods from the subclass Look but I cant reference variables.
Here is a link to a text hosting site, if anyone thinks it would be necessary for me to export and upload the package I guess I will but I feel like this might be just something I am not seeing that is obvious.
http://www.text-upload.com/read.php?t=1790

Your problem is your not actually referencing a variable from within Chaos, your referencing a variable from within Look.
i.e. you create a new Look() object with it's own instance of FSW which by default is initalised to null, this is never set inside Look
If you want to reference the variable in Chaos I suggest you pass the Chaos object into the Look's constructor.
So in look you would put a new field chaos, and add a constructor like so
public Look(Chaos chaos){
this.chaos = chaos
}
Inside Chaos when creating Look you would then do
new Look(this)
Inside look you could then reference chaos.FSW

Related

Why would you create a class instance using (CustomClass) "string" in Java?

I'm new to Java and working on a existing, big project. A couple times, I came across code like this: variable = (CustomClass) "string";.
Now, I really can't figure out why you would do that and how it is different from variable = new CustomClass("string");. The CustomClass has a constructor with one string argument.
In some situations, the above code just won't work which is why I came across it. But first, I want to understand what it does and Google doesn't seem to help. Or it is very possible I just don't know yet how to phrase the question accurately 😅
variable = (CustomClass) another_variable_reference;
Above line of code will tell JVM that another_variable_reference is-a class type of variable reference class, here CustomClass. So assign the reference to which another_variable_reference it is pointing to reference variable
variable = new CustomClass(another_variable_reference);
Above will create a new Object of CustomClass and assign the object reference to the variable
Hence, in the first case, we create a new reference which point's to an existing object while in second, we create a new Object and assign it's reference to variable

Calling constructor from another constructor and illegal argument exception

for my two parameter constructor to call the four parameter constructor is it proper with what I have done(I am still learning). Also it is supposed to instantiate open interval so is that still correct? for the copy constructor how would I make a copy of the explicit value constructor? Also how do I throw an exception if memory has not been allocated for object being copied?
for the copy constructor how would I make a copy of the explicit value constructor?
There is no built-in in Java for this. You'd have to manually copy the fields you want to copy
public LetsCallThisClassInterval(LetsCallThisClassInterval other){
this(other.left, other.right, other.stuff)
}
But this class looks like it should be immutable, so there is no real need for a copy constructor.
Also how do I throw an exception if memory has not been allocated for object being copied?
That just does not happen in Java. Memory is managed for you, and if you get an object reference, it will have been properly allocated already.
Or are you talking about other being null in the above example?
In that case, you will get a NullPointerException automatically. If you prefer an IllegalArgumentException (debatable), you can add a null check:
if (other == null)
throw new IllegalArgumentException("other interval cannot be null");
Let me see if I get this right, you want to do a constructor inception? In this case what you would want to do is use the Constructor(//insert the variables that correspond); that should fix the problem.
Using "this" will make the constructor refer to itself, I mean the same method. For example it would be.
public Constructor(char leftsym, double left, double right, char rightsymb){
new Constructor(left, right);
}
Now as a side note, the Constructor class already exists between the core classes in Java, I suggest that if you want to simplify your life change your java class name such as Constructor_1 or something like that.
Good Luck

References in a definition

I'm sitting here with an assignment that I don't quite understand - I hardly think it's that difficult, but I just don't know how to define the definition so that I make a reference to an object.
The assignment asks me to write a definition of a field named tutor that can hold a reference to an object of type Instructor.
I might not understand the question properly, but I'm fairly sure it's a rather easy assignment.
Thanks in advance.
If that's all the assignment, this is the solution:
public class MyClass{
Instructor tutor;
}
OK, when you create an object, you must have some way of holding on to it or you wouldn't be able to use it. A lot of times we define the reference and assign the value at the same time. What would happen, though, if you wanted to assign it later? Think about the typical creation of an object and what it would look like without doing the assignment, but just the definition.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3
The answer you want is in section 4.3.1
Objects are manipulated through references in Java. To declare a variable that holds a reference you use the syntax
NameOfType nameOfVariable;
The variable can optionally be initialized with the "null" reference or with a reference to an object.

modify value in variable

I have a problem with one class in java
this class is public and extends of DefaultHandler
all method of this class are public too ... but the variables are private...
My problem is that if I copy the value in other variable and modify this second variable the first change too.
is like static variables.. but they are no static... any idea!!!
thanks in advance
This is because you are actually modifying the same object. For instance, if you have
Object obj = new Object();
Object obj2 = obj;
You don't actually copy anything, you simply make obj2 "point" (not quite the right term, but it will work for now) to obj. Any changes to obj2 will be reflected in obj. Therefore, if you want to actually copy it, you need to physically create a new Object and then manually copy all of the values into the new creation. You could implement the prototype pattern to copy the object. Primitives don't behave this way so if you were to do the same thing with a double or an int for instance, it would behave the way you expect.
Does all of that make sense?
You are probably having a problem with passing by reference versus passing by value. This page explains what I mean http://www.cs.umd.edu/class/sum2004/cmsc420/sum4v3e01/node6.html.
You probably are copying a reference to a changeable object, not the object itself; so after the copy, you have two references to the same object. Changing that object through either reference will have the same effect.
I can't tell you how to copy the actual object because there's no generic way to do it, but many classes provide a copy constructor or some other way to duplicate themselves. If you need help with that you'd have to provide more details.

What "ClassName objectName;" does before the main method?

In this code I see the following lines before the "main" method:
JTextArea displayArea;
JTextField typingArea;
I wonder what these lines do and when they are executed. As far as I know the "main" method is the "entry point". So, the code will be executed from the beginning of the "main" method. All other methods will be executed if they are called from the "main" method. If it is so, the mentioned 2 lines will never be executed. Moreover, even if they will be executed, what exactly they do? What do these pairs of "ClassName objectName" do?
Those are called "declarations". They are declaring the existence of two variables, stating their types and their names. The location of the declaration determines their scope, in other words, which parts of the program are allowed to know about those particular variables, and may refer to them.
Here's a tutorial about Java variables.
You haven't included the whole file.
These are the declarations of fields. It indicates that when the class is instantiated (i.e., an object is created from it), each object will have a reference to a text area and a text field. You will, however, have to create these objects.
Notice how your main method is static. This means that it can run without the containing class being instantiated. However, most Java methods operate on objects. Suppose that your main is in class C. It is likely that somewhere in your main, you will see a "new C" which means that an instance of C is created. Then some other operation will be invoked on that new object (look for other, non-static methods in the file), and these operations will manipulate these two fields.
it specifies the types.
JTextArea displayArea; // this creates textarea type and thus textbox
JTextField typingArea; // thus creates textfield type var and thus text field
These are members (reference) variables of the KeyEventDemo class.
When KeyEventDemo is instantiated using the new keyword, each instance will have a these variables to refer to a JTextArea and JTextField respectively. These are initialized to null and are assigned as references to a couple of instances in the addComponentsToPane method.
Those declarations are evaluated when the object is instantiated by the java virtual machine. That is right before the main methode is this case.
Those are class members.
Basically, in a java class, you have methods and members. The members are variables that hold the state of Objects that are instances of that class.
The main method is separate, it's a static method so it can run without there being an instance of the class. So you have logic that runs in main() and you have logic that runs on an instance of the class. They're quite separate.
If you want too, in your main() function you can create an instance of the class, and then that could would 'run' if there is any initialization of the member functions that needs to be done.
However, not all class members are initialized in the constructor, and those would stay null (so in that case, nothing would 'execute' at that point). In your example, there's no initialization, so those members would be null when the constructor logic starts.

Categories