开发者

casting in Java

开发者 https://www.devze.com 2023-02-21 16:11 出处:网络
I wanted to know if there was a way to cast an array of integers into an array of characters in java. I keep getting a nullPointer Excepti开发者_如何学JAVAon error.

I wanted to know if there was a way to cast an array of integers into an array of characters in java. I keep getting a nullPointer Excepti开发者_如何学JAVAon error.

This is what I have ...

    private char [] clubsArray; // Will hold the clubsString String as an array 
        private char [] heartsArray;// Will hold the heartsString String as an array
        private char [] spadesArray;// Will hold the spadesString String as an array
        private char [] diamondsArray;// Will hold the diamondsString String as an array
    private int [] intClubsArray; // Holds the character array as number values so I can sort it properly 
        private int [] intHeartsArray; // Holds the character array as number values so I can sort it properly 
        private int [] intSpadesArray; // Holds the character array as number values so I can sort it properly 
        private int [] intDiamondsArray; // Holds the character array as number values so I can sort it properly 


public void cast()
    {
        for (int i = 0; i < clubsArray.length; i++)// A loop that will cast all the clubs into ints 
        {
            intClubsArray[i] = (int) clubsArray[i];
        }
        for (int i = 0; i < heartsArray.length; i++)// A loop that will cast all the hearts into ints 
        {
            intHeartsArray[i] = (int) heartsArray[i];
        }
        for (int i = 0; i < spadesArray.length; i++)// A loop that will cast all the spades into ints 
        {
            intSpadesArray[i] = (int) spadesArray[i];
        }
        for (int i = 0; i < diamondsArray.length; i++)// A loop that will cast all the diamonds into ints 
        {
            intDiamondsArray[i] = (int) diamondsArray[i];
        }
    }

Why am I getting this error ?? Can it be fixed ?? Can I cast an entire array ? and furthermore I dont want to set a specific size to the variable as I will get unnecessary 0 ' s for empty spaces when it becomes an int [] .


Is the error at clubsArray.length?

If so it would be because you have not done something like:

clubsArray = new char[12];

You could have also failed to initialize the intClubsArray or any other array for that matter)


Did you ever initialize your int*Array int arrays? Did you ever popular your *Array char arrays?

It looks like at least one of them is null, so you get an NPE when you call *Array.length. It might be helpful to post your exception's stacktrace.

To clarify my point, when you call something like

clubsArray.length

If the object (clubsArray) is null, you'll get a NullPointerException.


Casting is not what causing you the problem. You have to initialize each array of what it's size is with new. The code doesn't allocate memory for any of the arrays but calling the method length on the array is what causing the null pointer exception.

for (int i = 0; i < clubsArray.length; i++)
                 // ^^^^^^^^^^ Not initialized and is a null pointer
                 // But calling method on it is what the exception is saying.

So write a init method where you need to initialize arrays like -

clubsArray = new char[5] ;
// ....


Your real problem and cause of the NPEs is that you haven't initialized your arrays; see other answers for details:

But to answer the question that you actually asked:

I wanted to know if there was a way to cast an array of integers into an array of characters in java.

No. There is no way to do that in Java. The type system forbids it, and the Java runtime system won't let you break type safety to do that.

The best you could hope to do is:

  • cast element values that you fetch from the array (like you are currently doing!!), or
  • create a new array of the desired type and use a loop to initialize the elements with suitably cast values from the original array.

(Note that the 2nd is NOT equivalent to casting, since it involves creating and using a new array.)


Java doesn't have any built in way to cast an array in the way you want. It is easy though to write a method to do this.

My guess is that your problem is a failure to initialize the arrays. Java doesn't provide any way to use the type system to catch values being set to null, meaning that to build reliable code you have to be on the lookout for null at runtime. This is an annoyance also as it doesn't have to be that way (Spec#).

In that spirit, why not create a method which gracefully handless null arrays by passing them through unchanged. (Code not tested)

public int[] castArrayIntToChar(char[] toCast){
    if(toCast == null)
        return null;
    int[] casted = new int[toCast.length];
    for(int i = 0; i < toCast.length; i++){
        casted[i] = (int) toCast[i];
    }
    return casted
}

Having this built into the language would have one big benefit though: this is the kind of thing that can be super optimized using native code, vectorization, and loop unrolling. JVMs are pretty good these days, but a built in feature would allow much improved speed.

0

精彩评论

暂无评论...
验证码 换一张
取 消