Re: bit array and register problem
When you create an array, it allocates storage for the number of elements that you specified for the array in the new statement. The values in the slots are null until you give them values.
So Next you must assign values to each of the slots in the array.
It's a two step process: create the array and fill the array with objects.
Re: bit array and register problem
I tried filling the array with this code:
Code :
public Register(int numberOfBits)
{
Bit[] registerBit = new Bit[numberOfBits];
for (i = 0; i < numberOfBits; i++)
{
registerBit[i].reset();
}
}
When I run the debugger, I see that the program creates an array with numberOfBits of null values. That for loop should be giving a value of 0 for every i up to the last one but instead I get the error:
----jGRASP exec: java -Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,suspend=y,server=y RegisterTest
----jGRASP: connected to debugger.
Exception in thread "main" java.lang.NullPointerException
at Register.<init>(Register.java:119)
at RegisterTest.main(RegisterTest.java:5)
----jGRASP wedge2: exit code for process is 1.
Re: bit array and register problem
Quote:
I tried filling the array
The way to assign a value to an element in an array is this:
Code :
String[] strArray = new String[21]; // define an array
for(int i=0; i < strArray.length; i++){
strArray[i] = new String(); // assign a String object to the array element
}
You have never put a Bit object into the array at index i so its value is null. The value of registerBit[i] is null.
registerBit[i].reset();
Re: bit array and register problem
You need to read some more about arrays.
Go to this site: Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
and Find Arrays
Re: bit array and register problem
for this Code
Code :
public Register(int numberOfBits, int valueStored)
{
Bit[] registerBit = new Bit[numberOfBits];
for (i = 0; i < numberOfBits; i++)
{
registerBit[i] = new Bit();
}
String valueBinary = Integer.toBinaryString(valueStored);
for (i = 0 ; i < registerBit.length - 1;i++)
{
int start = i;
int end = i+1;
int value;
String valueString = valueBinary.substring(start, end);
value = Integer.parseInt(valueString);
if (value == 1)
{
registerBit[i].reset();
}
else
{
registerBit[i].set();
}
}
}
I want to create a toString() method that will return the string valueBinary. I tried doing this code:
Code :
public String showRegister()
{
return valueBinary;
}
but the code doesn't recognize the valueBinary in the showRegister() method. Is there a way where I can pull something from another method into this one?
Re: bit array and register problem
Quote:
Is there a way where I can pull something from another method into this one?
The valueBinary variable is defined local to the constructor. If you want to be able to use it in other methods, you need to move its definition to the class level.
You can define it in the class and give it a value in a method.
Re: bit array and register problem
Is there a way to form a string from an array? I want to concatenate all the values in order from Bit[i] to Bit[bit.length]
Re: bit array and register problem
There are methods in the Java API that can convert an array into a String. However the format is probably not what you what. So you need to roll your own. Use a StringBuilder, iterate over the array, append each element, call toString when done.
Re: bit array and register problem
Quote:
a way to form a string from an array
What do you want to see in the String?
Re: bit array and register problem
I got the string to work. I'm having an issue with the register now. Every time I create a new register, it saves over all the variables for the previous register. Is that a problem with the private variables?
Re: bit array and register problem
I finished the code thanks for all your help everyone.