Results 1 to 4 of 4
  1. #1
    ourimaler is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default Splitting program into multiple files

    My situation:
    For the past couple of years, I've been working on a certain program, stored in a file (let's call it Program.java for simplicity). It works fine, but it's gotten to the point where the file has about 1.5k lines of code. I can manage, but I imagine it'll be a nightmare to navigate for whoever replaces me sometimes down the road.

    So, I decided to try and split the program into several files, importing the other files into Program.java: One file would include the methods and classes involved with a certain functionality, one would include the methods and classes of another functionality...That kind of arrangement.
    That first idea ran into a wall when I realized the methods I was trying to move to one of those new files still called upon methods and global variables only present in Program.java. So I tried another idea: Create another file, called Tools.java, that would include all those global variables, and methods that could get called upon by other files.
    That...isn't working out either. I now have something that looks vaguely like this:

    Java Code:
    Program.java:
    
    import Tools.java
    
    public class Program
    {
        ClassName variable;  //a global variable
        public static void main(String[] args)
        {
            Program program = new Program();
            program.tools = new Tools(program.variable);
        }
    
        public void someMethod()
        {
            variable = new ClassName();
            tools.someOtherMethod();
        }
    ]
    and

    Java Code:
    Tools.java:
    
    public class Tools
    {
        ClassName variable;  //a global variable here, too
        public Tools(variable)
        {
            this.variable=variable;  //the goal is to make the Tools object use the global variable from Program
        }
    
        public void someOtherMethod()  //the goal is to allow other files to invoke tools.someOtherMethod()
        {
            variable.doSomething();
        }
    }

    When I launch this program, though, I get a java.lang.NullPointerException error at the "variable.doSomething();" line. According to a friend, this is because the variable gets initialized as null when I first declare it in Tools, rather than taking the value from the main program.

    So...at this point, I'm kinda stumped. Is there still a way to make this work? Am I making the whole multiple-file thing a lot more complicated than it actually needs to be?
    Last edited by ourimaler; 02-24-2012 at 10:36 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: Splitting program into multiple files

    Where, in Program, are you instantiating 'variable'?
    It's always null, in the code above anyway.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ourimaler is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default Re: Splitting program into multiple files

    Quote Originally Posted by Tolls View Post
    Where, in Program, are you instantiating 'variable'?
    It's always null, in the code above anyway.
    Right, sorry about that. Corrected.

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Splitting program into multiple files

    Perhaps you should try to focus on making your program strictly Object Orientated. Like you said, group methods and fields together based on functionality, and develop Objects from that. Seems like this is the kind of thing that OOP was developed for, and you might find that it benefits you hugely to follow the rules of it.

Similar Threads

  1. Select multiple files in IE
    By kirtichopra in forum Advanced Java
    Replies: 3
    Last Post: 02-03-2012, 06:33 AM
  2. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  3. run multiple jar files by one click
    By wallacetju in forum New To Java
    Replies: 2
    Last Post: 02-06-2011, 07:20 AM
  4. run multiple jar files by one click
    By wallacetju in forum Advanced Java
    Replies: 0
    Last Post: 02-06-2011, 06:50 AM
  5. Reading files and splitting them
    By Dieter in forum Advanced Java
    Replies: 5
    Last Post: 09-30-2009, 09:46 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
  •