Note that there are some explanatory texts on larger screens.

plurals
  1. POunable to use the methods of one java class into the second class using a package
    primarykey
    data
    text
    <p>Actually, my task is to make java bean classes inside a same package and use them into JSP (java server pages).</p> <p>Below is what i am doing.</p> <p><strong>Package name: mypack</strong> *<em>1st class name: UserBean.java</em>* (contains all getters and setters methods) <strong>2nd class name: Databean.java</strong> (contains the methods to interact with database)<br /> <strong>d:\java:</strong> jdk1.5 and tomcat, both are in this folder.</p> <p><strong>UserBean.java:</strong> I want to use the setter and getter methods of this class to set and get the values. <strong>DataBean.java:</strong> I want to use this class's methods to interact with MS Access database like fetch, update, delete etc. For these actions, i want to instantiate UserBean class object into DataBean Class to set and get the values of DataBean class. </p> <p>How i am compiling?</p> <p><strong>d:\java\mypack> set path=d:\java\jdk1.5\bin</strong><br /> <strong>d:\java\mypack>javac UserBean.java</strong></p> <p>But when i instantiate UserBean class's object inside the DataBean Class, i get the error that i have already mentioned.</p> <p><strong>d:\java\mypack>javac DataBean.java</strong><br /> here i get that above error message</p> <p>to resolve this issue, i modified the compiling line a little bit:</p> <p><strong>d:\java\mypack>javac -classpath .. DataBean.java</strong><br /> this works and successfully generates the class file</p> <p>For now, both classes are being compiled successfully. There is another issue now and is described below.</p> <pre><code>package mypack; **//UserBean.java** public class UserBean{ String username; //an attribute of UserBean Class String password; //an attribute of UserBean Class public UserBean(){} //Default Constructor of UserBean Class public void setUsername(String username){ this.username = username; }//setter method public String getUsername(){return username; } //getter method } package mypack; **//DataBean.java** public class DataBean{ UserBean user = new UserBean(); //Instantiating UserBean class object in //DataBean class String username; //an attribute of DataBean Class public DataBean(){} //Default Constructor of UserBean Class public String getUsernameTwo(){return user.getUsername(); } //gettter method } </code></pre> <p>On jsp page, i instantiated two objects </p> <pre><code>&lt;%@ page import="java.sql.*, mypack.*"%&gt; //importing packages &lt;html&gt; &lt;body&gt; &lt;% UserBean u = new UserBean(); *//UserBean class object u has been instantiated* DataBean d = new DataBean(); //DataBean class object d has been instantiated u.setUsername("John"); //username has been set as john out.println(u.getUsername()); //gets the string john, and prints it successfully (prints: John) out.println(d.getUsernameTwo()); //Does'nt print John, but prints null //**(it should print john too but it doesnt)** %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload