Results 1 to 2 of 2
  1. #1
    x2robbie2x is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Need help with abstract reference parameter.

    Java 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    this.bitString.add(bit);// * *<----- also an error here, NullPointerException,
    // * *I do not get why it is null though.

    Does this compile? (with or without the stars) bitString is not declared anywhere.

    If it does not compile and you can't understand the compiler message, copy and post the entire message, indicating to which line of your code the compiler is referring.

    ---------------------------

    When you post please use the "code" tags: put [CODE] at the start of the code and [/CODE] at the end. This will preserve the formatting when your post is displayed on the forum.

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2010, 05:57 AM
  2. Replies: 3
    Last Post: 09-16-2009, 10:27 PM
  3. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  4. Replies: 2
    Last Post: 11-21-2008, 01:20 AM
  5. Replies: 6
    Last Post: 10-27-2008, 01:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •