52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Al Hobbs wrote:Knowing where the null pointer is coming from is the first step to solving the problem. It should said the line where the exception occurred.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Junilu Lacar wrote:Not sure what you're trying to do here:
That's twice as much code as you need. Simplify it like this:
Ray Gilbert wrote:This is what happens:
Ray Gilbert wrote:nothing works.
I am using composition: Corvette327.java, "HAS A" L76Chevy327.java, "HAS A" MuncieM20CloseRatio.java, "HAS A" FuelTank.java.
I tried to combine L76Chevy327.java and MuncieM20CloseRatio.java additionally in a "HAS A" but I get null pointer. Is it just not possible to have this type of communication?
Ray Gilbert wrote:This is what happens:
Al Hobbs wrote:still no line number
Junilu Lacar wrote:If you follow my advice of only making small changes that you understand, this would be the way:
Norm Radder wrote:Can you include the full text of the compiler's error messages that have the source code line numbers?
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
What is the text of the error that happened at that line? Can you copy the full text of the message and paste it here?C:\...\CompositionTest\src\CompositionExample.java:4:24
Norm Radder wrote:
What is the text of the error that happened at that line? Can you copy the full text of the message and paste it here?C:\...\CompositionTest\src\CompositionExample.java:4:24
I am not familiar with an error message that has two : where the line number should be.
Where is the current source code for the CompositionExample class? I do not see any that have 24 lines.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Norm Radder wrote:The classes are contained within the CompositeExample class and only exist when there is an instance of the class.
main is a static method and can only access static members of the class.
Change the classes to static and the code in main will be able to access them. For example:
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Carey Brown wrote:This is all it takes for aggregation:
You could create a constructor that would initialize the otherClassReference but it isn't required.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Norm Radder wrote:You may need to have some set... methods to pass references to the classes that need them.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Ray Gilbert wrote:I just tried this:
Carey Brown wrote:This is all it takes for aggregation:
You could create a constructor that would initialize the otherClassReference but it isn't required.
Carey Brown wrote:
This style of programming is non-object oriented and is perpetuating a mess. You shouldn't need static methods unless they are just utilities that don't act on the program's objects.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
I get the NullPointerException meaning that the engine referred to in the transmission class is not the same engine in the car class,
Junilu Lacar wrote:
I get the NullPointerException meaning that the engine referred to in the transmission class is not the same engine in the car class,
No, that is not the reason. A NullPointerException, NPE for short, is thrown when you try to use a reference variable that hasn't been assigned a value. For example,
The key to getting classes to "talk" to one another is understanding that to invoke the methods of an object, you need a reference to it. If you want Engine to communicate with Transmission, you need to give/provide the Engine a reference to the Transmission object. Likewise, if you want an Accelerator to talk to an Engine, you need to give/provide the Accelerator a reference to the Engine. There are several ways to provide references: direct assignment, as a return value of a method call, setter/mutator method.
I do undertstand the concept, it's just that I need the car able to access all of it's component objects and in turn some of those objects able to access each other.
This is what it would take but it results in null objects:
As you can see, line 19 is null because the transmission and accelerator have not been initialized yet. No matter the order of lines 19-21, there are always two null objects.
Paul Clapham wrote:Well yeah, the engine needs the transmission's accelerator and the transmission needs the engine's accelerator. Are those supposed to be the same accelerator or are they different? And then you're creating a third accelerator? I don't know much about cars but all the cars I ever saw only had one accelerator...
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
John Gall wrote:“A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over with a working simple system.”
Junilu Lacar wrote:So here's my idea about this. Again, I want to start with really simple code that works.
a Car has an Engine.
a Car can be asked to display status, as it would on a dashboard.
You can tell a Car to turnOn(), turnOff(), and getTelemetry().
getTelemetry() will simply return a String that can be displayed, like so:
Engine: ON | OFF
The Car will start in a state where the Engine is OFF. If you call turnOn(), the Engine will be ON. If you call turnOff(), the Engine will be turned OFF.
That's it. Dirt simple start. Get that to work and you can add another feature to that but not before this little bit works and the code is clean enough to build upon.
The initial test for the Car class could be something like this:
You don't even have to start with any other class besides Car to get this test to work. Get the test to pass with a really naive implementation first, then change the code to incorporate the ideas of composition and delegation.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |