Need help with abstract reference parameter.
Code:
**************************************************
public class Driver
{
* Binary n1 = new Binary();
}
**************************************************
public class Binary extends BitString
{
* public Binary()
* * * *{
* * * * * * * *super();
* * * *}
}
***************************************************
public class BitString
{
* *public BitString()
* *{
* * * * BinaryBit temp = new BinaryBit();
* * * * this.addBit(temp); * * * * // * <----- there is an error here
* *}
* *public void addBit(AbstractBit bit)
* *{
* * * * *this.bitString.add(bit); * // * *<----- also an error here, NullPointerException,
* *} * * * * * * * * * * * * * * * // * *I do not get why it is null though.
}
****************************************************
public class BinaryBit extends AbstractBit
{
* * public BinaryBit()
* * {
* * * * super();
* * }
}
*****************************************************
public abstract class AbstractBit
{
* * public AbstractBit()
* * {
* * * * this(false);
* * }
* * public AbstractBit(boolean value)
* * {
* * * * *this.setBit(value);
* * } * * * * *
}
*******************************************************
In BitString there is a parameter that requires AbstractBit type, but the class is Abstract so I cannot initialize a reference to that. I created a reference to a subclass "BinaryBit" and initialized a referance variable to that, and passed it in the class. However I get an exception for the NullPointerException, and I don't know why. If anyone could help it'd be really nice of you :D