How can I use my array in another method.... Or better yet, what's wrong?

I guess I'll show you what I am trying to do rather and then explain it
public class arraycalc
int[] dog;
public void arraycalc()
dog = new int[2];
public void setSize(int size)
dog[1] = size;
public int getSize()
return dog[1];
This gives me a null pointer exception...
How can I use my array from other methods?

You have to make the array static. :)
Although I must admit, this is rather bad usage. What you want to do is use an object constructor to make this class an object type, and then create the array in your main class using this type, and then call the methods from this class to modify your array. Creating the array inside the other method leads to a whole bunch of other stuff that's ... well, bad. :)
Another thing: Because you're creating your array inside this class and you want to call your array from another class, you need to make the array static; to make it static, you must make your methods static. And according to my most ingenious computer science teacher, STATIC METHODS SUCK. :D
So, if you want to stick with your layout, it would look like:
public class arraycalc
     static int[] dog;
     public static void arraycalc()
          dog = new int[2];
     public static void setSize(int size)
          dog[1] = size;
     public static int getSize()
          return dog[1];
}But I must warn you, that is absolutely horrible code, and you shouldn't use it. In fact, I don't even know why I posted it.
You should definitely read up on OOP, as this problem would be better solved by creating a new object type.

Similar Messages

  • How can i use iphoto pics on another mac across network for screen saver?

    how can i use iphoto pics on another mac A across the home network for the screen saver on Mac B?
    within iPhoto i can see the mac A's shared library AOK on Mac B but Mac A's library doesn't show up in the screen saver list on Mac B ;-(
    tia
    Barry

    That's exactly what I tried to do, but the iPhoto librry doesn't show up in the pulldown menu. If I click "Choose Folder", the iPhoto library is greyed out and unable to be selected.
    For some reason, iPhoto is an option in the pulldown library on my older iMac but not on my MacBook Pro. What gives?

  • HT201359 How can I use PayPal as my payment method?

    How can I use PayPal as my payment method for my apple account?

    Contact iTunes Customer Service and request assistance
    Use this Link  >  Apple  Support  iTunes Store  Contact

  • How can we use Bounded Taskflow in another project.

    Greetings!
    Suppose we have 2 projects: projectA and projectB.
    In projectB we have 4 bounded taskflows: T1, T2, T3, T4.
    In projectA we have a view with a dynamic region, text input for digits and a command button.
    How can we fill our dynamic region with taskflows from projectB, by typing a number of it and pressing a button.
    Thank you.

    Hi,
    Yes, I know.
    However, I'm under the impression that this is not the preferred method. All the documentation describe the ADF library method and I cannot find any description about putting them in a separate project.
    All said, it seems to kind of work, using the 'jar dependencies', except for taskflows with a nested taskflow from another project. An I still need to import them as adf library.
    We'd like to develop our taskflows separately using a short development lifecycle. Putting each of them in a separate application and publish and import them in the main application seems to be too complicated and takes to much time, everytime.
    Am I correct that the shortest development lifecycle is to have all the taskflows in our webapplication, and have full debug and hot-swap functionality?
    Ciao
    Aino

  • How can I use my itunes on another computer?

    I am visiting my parents, who have itunes, and I want to access my itunes on their computer. How can I do this without disturbing their account.
    I'm also scared that if I plug my ipod in that it will sync with my Dad's itunes (that would be awful).

    You can't access your iTunes from their computer because your library is at home on your computer. You can only access there library.
    To plus an iPod into another computer, be sure to set your iPod (at your house before you leave) to Manual updating rather than automatic updating. This will allow the iPod to mount and iTunes will not do anything to what it on the iPod or in the iTunes library.
    Managing content manually on iPod
    http://docs.info.apple.com/article.html?artnum=61148
    Patrick

  • How can I use my account from another I.p address

    When ever I attempt to buy anything on amazon or iTunes, while at my mums using her iPad security locks me out, how do I fix this

    You don't. You cannot buy for your account using another person's iPad and using that person's account. iPads are not multi-user devices. Use your own iPad. You should be able to connect to your mum's wireless if she gives you the network's access password. Then you can buy stuff on your iPad.

  • How can i use one dtd inside another dtd

    Hello friends,
    I have read some where that one DTD can be imported into another DTD but idont remember how to do it.
    My question is suppose we have a DTD say xyz.dtd and i am writing a new DTD say abc.dtd, and lets assume that in abc.dtd i have to declare some of the elements of xyz.dtd, so iwant to import xyz.dtd into abc.dtd.
    Can anybody please give me a solution for this.
    Thank you,
    sridhar.

    something similar to this.
    // This is abc.dtd
    <!ELEMENT MESSAGE_NODE (MSGHEADER, MESSAGEBODY)>
    // Include the DTD for the Message Header
    <!ENTITY % MSGHEADER SYSTEM "xyz.dtd">
    %MSGHEADER;

  • My computer died. I do not want to loose one video on my iPod, how can I  use the iPod  on another computer? Please help!

    please advise how I can put my iPod on another computer, my old one died a slow death.  There is one short video on my iPod that can't  be replaced.

    - What is the source of the videos? If taken by the iPod you can transfer them to the new computer by:
    Copying personal photos and videos from iPhone, iPad, or iPod touch to your computer
    - If they were iTunes purchases by:
    iTunes Store: Transferring purchases from your iPhone, iPad, or iPod to a computer
    - If they were synced to the iPod and not iTunes purchases then you need a third-party program like TouchCopy or PhoneView.

  • How can I use a JavaBean in another JavaBean?

    I create a JavaBean named "dbBean.java"
    And I create another JavaBean named "login.java" which want to use the method defined in "dbBean.java".
    Could anyone tell me the syntax of using it?

    compile the .java files into .class files and then they are usable
    from dbBean...
    login myLogin = new login();
    perhaps you want to use it in some script and not java?

  • How can I use a module of another project?

    Hi
    I am using Visual studio 2013.
    I have a solution with 2 projects (console application).  In the first one there is a module that I want to use it in the other project (without compliantly recreate it in the second one, otherwise what is the benefit to have several projects in one
    solution?). is it a way to do that? 
    Thanks.

    Hi,
    You erroneously posted you question on the Project forum, which is dedicated to the scheduling and planning Microsoft application.
    Please go to the VS forum to have a proper help:
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=visualstudio
    Hope this helps,
    Guillaume Rouyre, MBA, MCP, MCTS |

  • How can I use the commandLink's setAction method?

    I have some jsf code like this:
    <af:commandLink text="#{row.name}" action="detail">
    and I want to change the commandLink's action dynamic,so I write such code:
    CoreCommandLink ccl=new CoreCommandLink();
    but I can't write
    ccl.setAction("detail");
    directly
    this method is like this
    public final void setAction(MethodBinding action)
    Can you give me some example how to use this method?
    Thank you very much!

    User,
    If you want the action to be dynamic, create a method in your backing bean that returns the proper navigation case string and bind the action to that. For example, if you have a backing bean called "myBean," write a method in the backing bean like this:
    public String myAction()
      if (something) return "action1";
      if (somethingelse) return "action2";
      // etc
    }and your commandLink looks like:
    <af:commandLink text="#{row.name}" action="#{myBean.myAction}"/>This is a much better way to do what you are trying. The error you are getting, by the way, is you are passing a string paramter to setAction, which expects a MethodBinding as a parameter; however, just use the way described above - much easier.
    John

  • How can I use the same apple id on 2 iphones yet have the owner contact different?

    My husband and I both share apps, contacts, calendars, etc and use same apple id...We both own iPhone 5's. When we each set up our iphones, on setting in siri setting and mail, contacts, calendars setting we each put contact for our info... Well the problem is that if I change mine to my info it changes it to his, and same if he changes it to his name on his iphone it changes 'my info' on my iPhone's setting.... so when using siri or mail... we cant identify each phone.... So can we still use same apple id and have separate "my info" settings.....

    Short term solution, do this on both devices
    Settings > iCloud > Shut off anything you don't want shared
    Long term solution (actual solution)
    You need seperate Apple IDs for iCloud. iTunes and iCloud are not the same.
    Create a new Apple ID > https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/
    On one of the devices go Settings > iCloud > Delete Account > Sign in with the new Apple ID
    This will seperate your Contacts, Calendars, Messages etc.
    You both can continue to use the same Apple ID for iTunes and share apps and music

  • Can i use an array as a methods parameter?

    If you can how would I call the method?
    public void method(int[] p){
    method({1, 1, 1 , 1});that gives me errors :D
    Edited by: tom14201 on Dec 16, 2007 5:19 AM

    sorry I meant in the last replyint arr[] = new int [4];
    for(int i= 0; i< 4; i++)
    arr[i] = 1;
    method(arr);Edited by: javadare on Dec 16, 2007 5:28 AM
    Edited by: javadare on Dec 16, 2007 5:30 AM

  • How can i use return statement in a method no flaming pls

    public class example2
    public int add(int i,int j)
    int k = i + j;
    return k; <<<< -HERE->
    public static void main(String[] args)
    example2 obj=new example2();
    obj.add(2,3);
    System.out.println("k"+k);
    Iam really really new to java programming so dont flame me
    In the above program i was hoping by putting return k it would return vlaue of k to the calling object but it didnt work i had to place sys.out.println in the method and take out the return from the method for it to work
    So why dosent it work and where is it returned to the k value.

    BlueRibbon wrote:
    In the above program i was hoping by putting return k it would return vlaue of k to the calling objectThat's what happens, yes. The VALUE of k is returned, not the NAME k, so you cannot refer to the value by just k. To the caller, the value is obtained by evaluating the expression that calls the function. So you could do
    public class example2
         public int add(int i, int j)
              return i + j;
         public static void main(String[] args)
              example2 obj = new example2();
              System.out.println(obj.add(2, 3));
    }

  • How can I use XSQLRequest for processing in-memory XMLDocument

    How can I use class XSQLRequest for processing in-memory XSQL?
    What should i pass in URL parameter value for constructor XSQLRequest(XMLDocument p0, URL p1)?
    Could you write the some examples?

    Here's one example of doing this:
    String xsqlPage = "<page connection='demo' xmlns:xsql='urn:oracle-xsql'>"+
                      "  <xsql:set-page-param name='username' value='{@u}'/>"+
                      "  <xsql:set-page-param name='password' value='{@p}'/>"+
                      "  <xsql:query bind-params='username password'>"+
                      "    select 'Authenticated' from dual"+
                      "    where 'SCOTT' = UPPER(?) /* username */"+
                      "      and 'TIGER' = UPPER(?) /* password */"+
                      "  </xsql:query>"+
                      "</page>";
      DOMParser d = new DOMParser();
      d.parse( new StringReader(xsqlPage));
      XSQLRequest req = new XSQLRequest(d.getDocument(),null);
      StringWriter sw = new StringWriter();
      PrintWriter errors = new PrintWriter(sw);
      Hashtable h = new Hashtable(2);
      h.put("u","scott");
      h.put("p","tiger");
      req.process(h,output,errors);
      output.println(sw.toString());

Maybe you are looking for