Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Passing an array to the constructor!

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Passing an array to the constructor!

    I implemented RC4 cryptography algorithm using Java but I still don't know why it doesn't work when I try to pass an array from main method to the constructor! How can I make it works?
    Thank you in advance

     
    package rc4.generator;
     
     
    public class RC4_Generator {
        private int[] S;
        private int[] T;
        private int keylen;    
     
        public RC4_Generator(byte[] key) throws Exception 
        {
                if (key.length < 1 || key.length > 256) 
                	throw new Exception("The key should be between 1 to 256 Byte!"); 
                else 
                	// The initialization of both S[] and T[].
                {
                        keylen = key.length;
                        for (int i = 0; i < 256; i++) {
                                S[i] = i;
                                T[i] = key[i % keylen];
                }
     
                    // The S permutation.
                        int j = 0;
                        for (int i = 0; i < 256; i++) 
                        {
                                j = (j + S[i] + T[i]) % 256;
                                S[i] = S[i] ^ S[j];
                                S[j] = S[j] ^ S[i];
                                S[i] = S[i] ^ S[j];
                        }
                }
        }
     
     
        public int[] encryption(int[] plaintext) 
        {
                int[] ciphertext = new int[plaintext.length];
                int i = 0, j = 0, k, t;
                for (int c = 0; c < plaintext.length; c++) {
                        i = (i + 1) % 256;
                        j = (j + S[i]) % 256;
                        S[i] = S[i] ^ S[j];
                        S[j] = S[j] ^ S[i];
                        S[i] = S[i] ^ S[j];
                        t = (S[i] + S[j]) % 256;
                        k = S[t];
                        //ciphertext[c] = plaintext[c] ^ k;
                }
                return ciphertext;
        }
     
     
        public static void main(String[] args){
     
        	byte[] k ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
     
        	RC4_Generator rc4= new RC4_Generator(k);
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Passing an array to the constructor!

    it doesn't work
    Please explain what "doesn't work" means.
    Copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Passing an array to the constructor!

    You've commented to initialize S[] and T[], but do you actually initialize them anywhere? An array needs to be initialized with a length.
    Also, when asking a question, make sure to tell us what is going wrong. "It doesn't work" is not a helpful statement for us. Is the output different than you expect, and how is it different? Are you getting exceptions or errors, and what type of exceptions and errors?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing an array to the constructor!

    Oh! I'm sorry for that! I'm a new user here! I will be clear

    I got this error:
    -----------------------------------------------------------------
    Exception in thread "main" java.lang.NullPointerException
    at rc4.generator.RC4_Generator.<init>(RC4_Generator.j ava:18)
    at rc4.generator.RC4_Generator.main(RC4_Generator.jav a:62)
    ----------------------------------------------------------------
    Do you see guys why? I try to understand it but I couldn't!

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Passing an array to the constructor!

    I would guess because S[] and T[] have not been initialized, but you attempt to add values to them.
    You need to initialize them beforehand in your constructor like this:
    S = new int[256]; // 256 is the size of the array, you can change this to whatever length seems appropriate
    T = new int[256];
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing an array to the constructor!

    It worked!
    Thank you so much (aussiemcgr ) for your help! Really Appreciate that

Similar Threads

  1. [SOLVED] Passing 2D array to method
    By drgroove777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2012, 02:08 AM
  2. Array in Constructor
    By spark in forum Collections and Generics
    Replies: 5
    Last Post: July 11th, 2012, 12:22 PM
  3. Please help with passing an array to a new class
    By axelrose in forum Object Oriented Programming
    Replies: 1
    Last Post: July 6th, 2012, 05:08 PM
  4. HELP. Passing Array to class.
    By videodrome in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 13th, 2010, 04:35 AM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM