Nested Classes - Null Pointer Exception
in
Programming Questions
•
3 months ago
I'm trying to nest a class within a class. The reason is I need to store a series of pieces of information (an integer, and a boolean value) multiple times within the main class. In C++ i'd use a struct, but it doesn't seem that java has the same option.
Thanks
I've been getting a Null Pointer Exception error and searched all over the internet about how to handle nested classes when we need
multiple instances of the inside one
, but didn't have much luck. Line 25 is where the error is occuring. I tried multiple versions, and this is just the latest one that I have after stumbling through different examples.
I may be doing this all wrong. Is there a better way I can store pairs of variables (such as a milliseconds as an integer, and a true/false value as a boolean) and easily iterate through them? I originally used 2 arrays (one for time, and one for the true/false value) but it didn't work as well as I'd like. When I look through I want to get the integer value from smallest to largest, regardless of whether it has a true or false value. I'm thinking I could also try encoding the boolean value into a single integer along with the other integer value but that seems like too much of a hack.
(On a side note, I know that I'll need to have code to expand the number of instances. I haven't researched that part yet since this part has me stumped.
Thanks
- class LedChannel {
- private int pin;
- private boolean alreadycounted;
- private int lastused;
- private Timeline[] timing;
- public LedChannel(int xpin) {
- this.pin = xpin;
- this.alreadycounted = false;
- this.lastused = 0;
- Timeline[] timing = new Timeline[1];
- timing[0] = new Timeline();
- }
- public class Timeline() {
- public int currtime;
- public boolean ledstatus;
- }
- public void addvalue(int tmptime, boolean tmpstatus ) {
- // The line below this errors with a NULL POINTER EXCEPTION
- timing[lastused].currtime = tmptime;
- timing[lastused].ledstatus = tmpstatus;
- lastused++;
- }
- }
1