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 14 of 14

Thread: Implementing Interface

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Implementing Interface

    Hi all,
    I am doing one project called PERSONAL EXPENSES DATABASE,it has a more than 10 modules.some of that MAINLOGIN,DATAMANIPULATORVIEW,UPDATERVIEW,STOREDAT A,UPDATER,UPDATERVIEW and so on.

    DATAMANIPULATORVIEW is used to store data in the database as well as displaying the existing data in database,so that i have created a interface called DATADISPLAYER and it also implemented by DATAMANIPULATORVIEW.

    DATADISPLAYER has two method declaration called retrieveData(),displayData().

    i write structure of two classes on which i got the problem,

    1.
    public class DataManipulatorView,implements ActionListener,Runnable,DataDisplayer{
    //some text field's,lable's and button's

    public DataManipulatorView(){
    }
    public void run(){
    }
    public void addComponent(){
    }
    public void actionPerformed(){
    }
    public void retrieveData(){
    //retrieve data from database and store it on local variables
    }

    public void displayData(){
    all text field's are filled with local variables using setText()//here i got null pointer exception
    }

    public static void main(){
    }
    }
    2.
    public class UpdaterView{
    //instance variable declaration


    public UpdaterView(){
    }
    public void run(){
    }
    public actionPerformed(ActionEvent e){
    if(e.getSource()==update){
    DataManipulaterView dm=new DataManipulaterView();
    dm.retrieveData();
    dm.displayData();//when calling i got null pointer exception
    }

    public static main(){
    }
    }

    I bold some parts on above program in which i am facing an error
    kindly help me
    thank you


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    My Mood
    Sleepy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Interface

    To handle the Exceptions in your project, you need to use "try&catch" blocks in your code.Try block is for" whatever the statements that are rising to an exception place them in try block ,the try block duty is to catch that exception class object and transfers it to catch block.
    Syntax:
    try
    {
    //statements that are rising exceptions

    }
    catch(NullPointerException e)//here null pointer exception is free defined method to catch the exception object
    {
    e.printStackTrace();
    }

    I Hope that this will help you.If not sorry.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Implementing Interface

    //here i got null pointer exception
    What is null on that line? The stack trace tells you exactly which line throws the exception, look at the variables accessed on that line to see which is null (us System.out.println if you have to).

    I would discourage catching the exception as recommended in post #2 - while catching and dealing with exceptions is an important thing to know, RuntimeException's are typically a result of misuses of API's and/or coding errors and should be dealt with rather than caught.

  4. The Following User Says Thank You to copeg For This Useful Post:

    mohamed_mashood (June 27th, 2013)

  5. #4
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Interface

    Please i need help on this issue,i could not understand the problem

    This class is DataManipulatorView

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.sql.*;
    import javax.swing.border.*;
    import java.util.regex.*;
    import java.util.*;

    interface DataDisplayer{

    public void retrieveData();
    public void displayData();

    }
    public class DataManipulatorView implements ActionListener,Runnable,DataDisplayer {

    String title,butname,criteria_date,criteria_day,name_user ;
    String other_exp,money_lended,date_field,day_field,lunch_ field,supper_field,breakfast_field;
    JFrame mainframe;
    JLabel break_fast,lunch,supper,date,day,othr,mny;
    JTextField breakfast,lunch_food,supper_food,date_text,day_tex t,othr_exp,mny_lnd;
    JPanel main_panel,date_panel,food_panel,others_panel;



    public DataManipulatorView(){

    }

    public DataManipulatorView(String title,String butname,String criteria_date,String criteria_day){
    this.title=title;
    this.butname=butname;
    this.criteria_date=criteria_date;
    this.criteria_day=criteria_day;
    }


    public void run(){
    mainframe=new JFrame(title);
    mainframe.setLocationRelativeTo(null);
    try{
    name_user=b.getUsername();
    }
    catch(Exception ex){
    }
    main_panel=new JPanel(){


    public void paintComponent(Graphics g){
    g.drawImage(im,0,0,null);
    setSize(800,700);
    g.drawString("Welcome:"+name_user,600,20);
    }
    };


    mainframe.setContentPane(main_panel);
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
    mainframe.setVisible(true);
    mainframe.setLocation(243,14);
    mainframe.setSize(800,700);
    main_panel.setLayout(null);
    gb=new GridBagLayout();
    gbc=new GridBagConstraints();
    gbc.fill=GridBagConstraints.NONE;



    date_panel=new JPanel();
    date_panel.setBackground(Color.white);
    date_panel.setBorder(BorderFactory.createTitledBor der("Date Identification"));
    date_panel.setLayout(gb);
    date_panel.setPreferredSize(new Dimension(350,150));
    date=new JLabel("DATE:");
    Insets i=new Insets(0,0,20,0);
    addComponent(0,0,0.5,0.5,date,date_panel,i);
    date_text=new JTextField(20);
    i=new Insets(0,0,20,0);
    addComponent(1,0,0.5,0.5,date_text,date_panel,i);
    day=new JLabel("DAY:");
    i=new Insets(0,0,20,0);
    addComponent(0,1,0.5,0.5,day,date_panel,i);
    day_text=new JTextField(20);
    i=new Insets(0,0,20,0);
    addComponent(1,1,0.5,0.5,day_text,date_panel,i);

    date_panel.setBounds(20,40,350,150);

    food_panel=new JPanel();
    food_panel.setBackground(Color.white);
    food_panel.setLayout(gb);
    food_panel.setPreferredSize(new Dimension(350,150));
    food_panel.setBorder(BorderFactory.createTitledBor der("Food Details"));
    break_fast=new JLabel("BREAK FAST:");
    i=new Insets(0,0,0,10);
    addComponent(0,0,1,1,break_fast,food_panel,i);
    breakfast=new JTextField(20);
    i=new Insets(0,0,0,0);
    addComponent(1,0,1,1,breakfast,food_panel,i);
    lunch=new JLabel("LUNCH:");
    i=new Insets(0,18,0,0);
    addComponent(0,1,1,1,lunch,food_panel,i);
    lunch_food=new JTextField(20);
    i=new Insets(0,0,0,0);
    addComponent(1,1,1,1,lunch_food,food_panel,i);
    supper=new JLabel("SUPPER:");
    i=new Insets(0,15,0,5);
    addComponent(0,2,1,1,supper,food_panel,i);
    supper_food=new JTextField(20);
    i=new Insets(0,0,0,0);
    addComponent(1,2,1,1,supper_food,food_panel,i);

    food_panel.setBounds(400,40,350,150);

    others_panel=new JPanel();
    others_panel.setBackground(Color.white);
    others_panel.setPreferredSize(new Dimension(350,150));
    others_panel.setBorder(BorderFactory.createTitledB order("Other Details"));
    others_panel.setLayout(gb);
    othr=new JLabel("OTHRE EXPENSES:");
    i=new Insets(0,0,0,0);
    addComponent(0,0,0.5,0.5,othr,others_panel,i);
    othr_exp=new JTextField(20);
    i=new Insets(0,0,0,0);
    addComponent(1,0,0.5,0.5,othr_exp,others_panel,i);
    mny=new JLabel("MONEY LENDED:");
    i=new Insets(0,0,0,0);
    addComponent(0,1,0.5,0.5,mny,others_panel,i);
    mny_lnd=new JTextField(20);
    i=new Insets(0,0,0,0);
    addComponent(1,1,0,0,mny_lnd,others_panel,i);

    others_panel.setBounds(20,220,350,150);

    save=new JButton(butname);save.setBounds(100,400,80,30);
    reset=new JButton("RESET");reset.setBounds(300,400,80,30);
    cancel=new JButton("CANCEL");cancel.setBounds(500,400,80,30);
    save.addActionListener(this);
    reset.addActionListener(this);
    cancel.addActionListener(this);

    mainframe.setJMenuBar(mb);
    main_panel.add(date_panel);
    main_panel.add(food_panel);
    main_panel.add(others_panel);
    main_panel.add(save);
    main_panel.add(reset);
    main_panel.add(cancel);

    }//run method closed


    public void actionPerformed(ActionEvent e){


    if(e.getActionCommand()=="SAVE"){
    sd=new StoreData();
    sd.getBeanObject(b);
    sd.storeData(date,day,breakfst,lun,sup,other,mony) ;

    }
    if(e.getActionCommand()=="UPDATE"){
    Updater up=new Updater();
    up.updateData(breakfst,lun,sup,other,mony);

    }

    }
    if(e.getSource()==update_item){
    UpdaterView uv=new UpdaterView();
    SwingUtilities.invokeLater(uv);
    }
    if(e.getSource()==cancel){
    System.exit(0);
    }

    }
    public void retrieveData(){


    db=new DBconnection();
    try{
    con=db.connectDB();
    stmnt=con.createStatement();
    rs=stmnt.executeQuery("select * from expenses where DATE='"+criteria_date+"' and DAY='"+criteria_day+"'");
    while(rs.next()){
    String date_field=rs.getString("DATE");
    String day_field=rs.getString("DAY");
    String breakfast_field=Integer.toString(rs.getInt("BREAKF AST"));
    String lunch_field=Integer.toString(rs.getInt("LUNCH"));
    String supper_field=Integer.toString(rs.getInt("SUPPER")) ;
    String money_lended=Integer.toString(rs.getInt("MONEY_LEN DED"));
    String other_exp=Integer.toString(rs.getInt("OTHER_EXPENS ES"));
    String total_exp=Integer.toString(rs.getInt("TOTAL_EXPENS ES"));
    String food_exp=Integer.toString(rs.getInt("FOOD_EXPENSES "));

    date_text.setText(date_field);//this place am getting null pointer exception
    day_text.setText(day_field); //it can display data on command prompt at the same time when i try to
    breakfast.setText(breakfast_field);// set the value on any textfield,i get null pointer exception
    lunch_food.setText(lunch_field);
    supper_food.setText(supper_field);
    othr_exp.setText(other_exp);
    mny_lnd.setText(money_lended);

    }

    }
    catch(Exception sql){
    sql.printStackTrace();
    }
    }


    public static void main(String args[])
    {
    DataManipulatorView dm=new DataManipulatorView("DataManipulator","SAVE");
    SwingUtilities.invokeLater(dm);
    }
    }

    This is UpdaterView Class

    import java.io.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;

    public class UpdaterView implements ActionListener,Runnable{

    JLabel date,day;
    protected JTextField date_text,day_text;
    JButton update,cancel;
    JPanel frame_panel,main_panel;
    JFrame frame;
    Image im;
    GridBagLayout gb;
    GridBagConstraints gbc;

    public UpdaterView(){


    }

    public void run(){
    frame=new JFrame();
    frame.setTitle("Update Expenses-Personel Expenses DataBase");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_C LOSE);
    frame.setVisible(true);
    frame.setSize(800,700);

    im=new ImageIcon("hp-invent-blue-horizon-wallpaper.jpg").getImage();
    frame_panel=new JPanel(true){
    public void paintComponent(Graphics g){
    setSize(800,700);
    g.drawImage(im,0,0,null);
    }
    };
    frame.setContentPane(frame_panel);
    frame.setLayout(null);

    main_panel=new JPanel(true);
    main_panel.setBackground(Color.white);
    main_panel.setBorder(BorderFactory.createTitledBor der("Updating Details"));
    gb=new GridBagLayout();
    gbc=new GridBagConstraints();
    gbc.fill=GridBagConstraints.NONE;
    main_panel.setLayout(gb);

    date=new JLabel("DATE:");
    Insets in=new Insets(0,0,0,0);
    addComponent(0,0,1,1,date,main_panel,in);
    date_text=new JTextField(10);
    in=new Insets(0,0,0,0);
    addComponent(1,0,1,1,date_text,main_panel,in);
    day=new JLabel("DAY:");
    in=new Insets(0,0,0,0);
    addComponent(0,1,1,1,day,main_panel,in);
    day_text=new JTextField(10);
    in=new Insets(0,0,0,0);
    addComponent(1,1,1,1,day_text,main_panel,in);
    update=new JButton("UPDATE");
    update.addActionListener(this);
    in=new Insets(0,25,0,0);
    addComponent(0,2,2,2,update,main_panel,in);
    cancel=new JButton("CANCEL");
    cancel.addActionListener(this);
    in=new Insets(0,0,0,0);
    addComponent(1,2,2,2,cancel,main_panel,in);

    main_panel.setBounds(250,250,300,150);
    frame_panel.add(main_panel);

    }
    public void addComponent(int x,int y,int wx,int wy,Component comp,Container cont,Insets ins){
    gbc.gridx=x;gbc.gridy=y;
    gbc.insets=ins;
    gbc.weightx=wx;gbc.weighty=wy;
    gb.setConstraints(comp,gbc);
    cont.add(comp);
    }

    public void actionPerformed(ActionEvent e){

    if((e.getSource()==update)){
    String date=date_text.getText();
    String day=day_text.getText();
    DataManipulatorView dm=new DataManipulatorView("Updator-Personel Expenses Database","UPDATE",date,day);
    dm.retrieveData(); //when calling there is null pointer exception
    SwingUtilities.invokeLater(dm);
    }
    if((e.getSource()==cancel)){
    frame.dispose();
    }

    }

    public static void main(String args[]){
    UpdaterView uv=new UpdaterView();
    SwingUtilities.invokeLater(uv);
    }
    }
    Actually what i need is ,when i click update button on UpdaterView class it should the retrive the data from database and set all the values in all the textfield's.
    can any one tell me what is the error in the code,please forgive if you find any inconvenience on the above program.

    Thank you so much
    Last edited by mohamed_mashood; June 27th, 2013 at 02:55 AM. Reason: Additional sentence

  6. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Implementing Interface

    Format your code, please. That is unreadable.

  7. #6
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Access insatance variables

    i hope you have seen my attachment.i found the source of exception,they are the text field's of the class DataManipulatorView.

    Take a look at the below sample code

    public interface DataDisplayer{

    public void retrieveData()

    }

    public class DataManipulator implements DataDisplayer{

    JTextField date,day;

    public void run(){

    date= new JTextField(20);
    day=new JTextField(20);


    }
    public void retrieveData(){

    //retrieve data form database and store that in local variables

    date.setText(localvariable);//date instance is the reason for the null pointer exception,is it possible to access the date instance from here?
    day.setText(localvariable);

    }
    }

    public class UpdaterView{
    JButton update;

    public void run(){
    update=new JButton("Update");
    }
    public void actionPerformed(ActionEvent e){

    if(e.getSource()==Update)
    {
    DataManipulatorView dm=new DataManipulatorView()
    dm.retrieveData();//invoke the retrieveData method on DataManipulatorView class
    dm.setVisible(true);

    }
    }
    }
    I hope you could understand the above code,please give me the solution

    Thank you

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Interface

    I Have attached two program's as the text format,kindly help me.dont ignore me please
    Attached Files Attached Files

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Implementing Interface

    Quote Originally Posted by mohamed_mashood View Post
    I Have attached two program's as the text format,kindly help me.dont ignore me please
    Please post code on the forum within code tags, and not as an attachment.
    See the Announcements page if you need help.
    Most people will not bother to read short code that is not formatted, who did you expect to read the lengthy code you posted in that condition?

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Interface

    I am really sorry about this,Please refer below

    interface DataDisplayer{
     
    public void retrieveData();
    public void displayData();
     
    }
    public class DataManipulatorView implements ActionListener,Runnable,DataDisplayer {
     
    JTextField breakfast,lunch_food,supper_food,date_text,day_tex t,othr_exp,mny_lnd;
     
    public void run(){
     
    date_text=new JTextField(20);
     
    day_text=new JTextField(20);
     
    breakfast=new JTextField(20);
     
    lunch_food=new JTextField(20);
     
    supper_food=new JTextField(20);
     
    othr_exp=new JTextField(20);
     
    mny_lnd=new JTextField(20);
     
    }//run method closed
     
    public void retrieveData(){
    // retrieve some data from database
    date_text.setText(date_field);//this place am getting null pointer exception
    day_text.setText(day_field); //it can display data on command prompt at the same time when i try to 
    breakfast.setText(breakfast_field);// set the value on any textfield,i get null pointer exception
    lunch_food.setText(lunch_field);
    supper_food.setText(supper_field);
    othr_exp.setText(other_exp);
    mny_lnd.setText(money_lended);
    }
     
    }
    catch(Exception sql){
    sql.printStackTrace();
    }
    }
     
    }

    public class UpdaterView implements ActionListener,Runnable{
     
    	//remaining codes
    public void actionPerformed(ActionEvent e){
     
    	if((e.getSource()==update)){
    		String date=date_text.getText();
    		String day=day_text.getText();
    		[B]DataManipulatorView dm=new DataManipulatorView("Updator-Personel Expenses Database","UPDATE",date,day);
    		dm.retrieveData();//invoke the over ridden method that retrieve all  data from the database
    		SwingUtilities.invokeLater(dm);[/B]
    		}
    	if((e.getSource()==cancel)){
    		frame.dispose();
    	}
     
    	}
     
    	public static void main(String args[]){
    		UpdaterView uv=new UpdaterView();
    		SwingUtilities.invokeLater(uv);
    }
    }
    I posted the problematic code only above
    I found the reason of null pointer exception,they are textfield reference's but i dont know how to resolve

  11. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Implementing Interface

    Quote Originally Posted by mohamed_mashood View Post
    ... I found the reason of null pointer exception,they are textfield reference's but i dont know how to resolve
    You have to make sure the variables are initialized before they are used.

    Quote Originally Posted by mohamed_mashood View Post
    //it can display data on command prompt
    What data does it display?

    Check that retrieveData is not being called before run where the NullPointerException is thrown. If it is, initialize the variables earlier, or wait to use them until they have been initialized.

  12. #11
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Interface

    String data(date,day,breakfast,lunch,supper,other expenses and money borrowed) to display on that corresponding field's.

    I already created the object for all text field's on the run method then why it throw null pointer exception?

  13. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Implementing Interface

    Did you read the last part of post #10?

  14. #13
    Junior Member
    Join Date
    Mar 2013
    Location
    trichy
    Posts
    28
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Implementing Interface

    yes i did firend

  15. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Implementing Interface

    Quote Originally Posted by mohamed_mashood View Post
    yes i did firend
    Then add some printlns to the code. Step through it with a debugger. What ever method you use...
    Get from the point where the program start to where the null pointer exception is thrown. Then look back to see why that variable was not initialized. I do not know how to better answer this question than what has been said. Short of rewriting the code which is not going to happen.
    If you have a different question please post it. Otherwise you have all you need to fix this.

Similar Threads

  1. Implementing a webproxy
    By arsenewenger in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 12th, 2013, 10:19 AM
  2. Implementing a webproxy
    By arsenewenger in forum Java Networking
    Replies: 5
    Last Post: February 12th, 2013, 10:19 AM
  3. implementing KeyListener
    By FARGORE in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2012, 08:32 AM
  4. implementing interface from encrypted jar
    By mark111 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 25th, 2010, 01:38 PM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM