Constructor parameters issue

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 866 Views - Last Post: 23 June 2009 - 09:14 PM Rate Topic: -----

#1 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Constructor parameters issue

Post icon  Posted 22 June 2009 - 12:32 AM

Hi,

I have fixed some parameters to my constructor but unable to make them work.

Here is my sample code:-

 public MyConstructor(String filename, int value1, int value2){

		   this.filename = filename;
		   this.value1 = value1;
		   this.value2 = value2;

		   File reading code
		   BufferedReader fh = new BufferedReader(new FileReader(filename)); 
			  ................

				 }

			  public static void main(String args[]) {
					   String s1 = args[1];
					   String s2 = args[2];

					   int value1 = Integer.parseInt(s1.trim());
					   int value2 = Integer.parseInt(s2.trim());

		 MyConstructor myobject = new MyConstructor(args[0], value1, value2);

													 }




Can anybody help me to fix this problem.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Constructor parameters issue

#2 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Constructor parameters issue

Posted 22 June 2009 - 12:41 AM

If your instance variables have the same name as the parameters, change the parameters. If that's not the solution to your problem then can you post your problem in more detail? Do you get any errors?
Was This Post Helpful? 0
  • +
  • -

#3 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Re: Constructor parameters issue

Posted 22 June 2009 - 10:04 AM

If I create a method like
 public void ReadFile(String filename, int Value1, int value2) {
	this.filename = filename;
	this.value1 = value1;
	this.value2 = value2;
.........................
	BufferedReader fh = new BufferedReader(new FileReader(filename));
 ............................................  
 }
  


Now if I want to call it in my previous example constructor like
public Constructor() {

...........................

  //How do I call my method ?
  //I am doing like this

   ReadFile(filename, range1, range2);
 }



and then I will call this method in the main as:-
public static void main(String args[]) {
	 
		 String s1 = args[1];
		 String s2 = args[2];
		 String filename = args[0];

		 int value1 = Integer.parseInt(s1.trim());
		 int value2 = Integer.parseInt(s2.trim());
  
	   MyClass exmpl = new MyClass();
	   exmpl.ReadFile(filename, value1, value2) 
	  /// Will it work.. If not howto do then ?
}




Thanks
Was This Post Helpful? 0
  • +
  • -

#4 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Re: Constructor parameters issue

Posted 22 June 2009 - 10:25 AM

Most important am I calling the method correctly ? in the constructor ?
ReadFile(filename, value11, value2);



I am a bit confused about howto call a method inside a constructor and how do I handle exception in this case using this example reading file. I am already using try catch inside my ReadFile method.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: Constructor parameters issue

Posted 22 June 2009 - 11:03 AM

What is the name of your class saved as? You *are* calling the method correctly, but as a convention method names being with a lowercase letter. something like readFile or readfile would be more appropriate.
Was This Post Helpful? 0
  • +
  • -

#6 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Re: Constructor parameters issue

Posted 22 June 2009 - 11:30 AM

I am actually using a class inside a class. Can anybody point out my mistake. My code is compiling OK but when I run it. It gives me NullPointException at java.io.FileInputStream<init>. Like this one. Need help.

The basic structure of my code is like this:-

public class Class1 extends JFrame{

   public Class1(){this.somevalues
  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setMinimumSize(new Dimension(400, 400));
	  class2obj = new Class2();
	  getContentPane().add(class2obj, BorderLayout.CENTER);
	  pack();
	  }
	  }

   static class Class2  extends JPanel implements MouseMotionListener {
   
   public String filename;
   public int value1;
   public int value2;

  public Class2(){
	 super();
	 readFile(filename, value1, value2);  
	   }
  public readFile(String filename, int value1, int value2){
	   this.filename = filename;
	   this.value1 = value1;
	   this.value2 = value2;
	   ............
	   try{
			BufferedReader fh = new BufferedReader(new FileReader(filename)); 
.............................
  fh.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
...............

public static void main(String args[]) {
		 String s1 = args[1];
		 String s2 = args[2];
		 String filename = args[0];
		 
		 int value1 = Integer.parseInt(s1.trim());
		 int value2 = Integer.parseInt(s2.trim()); 
		  
		 Class2 obj = new Class2();
		 obj.readFile(filename, value1, value2);
		 
	  java.awt.EventQueue.invokeLater(new Runnable() {
	  public void run() {
	  new Class1().setVisible(true);
	  }
	  });
	  }
 } 





Thanks
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,254
  • Joined: 27-December 08

Re: Constructor parameters issue

Posted 22 June 2009 - 12:37 PM

Your BufferedReader that you declare inside the try statement will be local to that try. To increase the scope of your variable, declare it outside your try statement. Also, the String filename has not been initialized. Just b/c you call the method you've defined as readFile doesn't mean it is locating or giving you a file name. And on a side note, your syntax and conventions are a little sloppy. You should look into cleaning up your code. If you need help with that, let us know! Good luck with your project!
Was This Post Helpful? 0
  • +
  • -

#8 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Re: Constructor parameters issue

Posted 23 June 2009 - 04:08 AM

View Postmacosxnerd101, on 22 Jun, 2009 - 11:37 AM, said:

Your BufferedReader that you declare inside the try statement will be local to that try. To increase the scope of your variable, declare it outside your try statement. Also, the String filename has not been initialized. Just b/c you call the method you've defined as readFile doesn't mean it is locating or giving you a file name. And on a side note, your syntax and conventions are a little sloppy. You should look into cleaning up your code. If you need help with that, let us know! Good luck with your project!


I am getting these errors:-
Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at ClassInner1$InnerClass.readFile.(Class1 java : 95)
at ClassInner1$InnerClass<init>(Class1 java : 85)
at Class1.main.(Class1 java : 200)


Here are these troubling lines:-
readFile(filename) - line 85
fh = new BufferedReader(new FileReader(filename)); - line 95
InnerClass test = new InnerClass(); - line 200


I need your kind guidance here. Unable to figure out how to solve this one. Maybe bcoz of my inexperience.

Hi here is my full code:-

import java.io.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;

	  class Class1 extends JFrame {
			  public Color color;
			  public int skeeing;
			  public int myfix1;
			  public int myfix2;
			  public int width;
			  public String text;
			  public int end;

		   
			  private JPanel InnerClass;
			  public Class1(){
				  
			  }

	  public Class1(int skeeing, int myfix1, int width, int myfix2, Color c,String text) {
		this.color = c;
		this.skeeing = skeeing;
		this.myfix1 = myfix1;
		this.width = width;
		this.myfix2 = myfix2;
		this.text = text;

	  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setMinimumSize(new Dimension(400, 400));
	  InnerClass = new InnerClass();
	  getContentPane().add(InnerClass, BorderLayout.CENTER);
	  pack();
	  }
	  static class InnerClass extends JPanel implements MouseMotionListener, MouseListener {
	  public List<Glyph> glyphs;
		  public int skeeing;
		  public int bottom;
		  public int width;
		  public String f[];
		  public int value1;
		  public int value2;
		  private final static int NUM_FIELDS = 12;
		  BufferedImage image;
	  Graphics2D g2d;
		  Point startPoint = null;
		  Point endPoint = null;
		 
		  public int start;
		  public int x;
		  public int y;
		  int scaledvalue;
		  public int end;
		 
		  
		  
		  
		  
		public InnerClass(){
 
		   super();
		   String filename = null;
		   addMouseMotionListener(this);
		   addMouseListener(this);
		   boolean mouseClicked = false;
		   //public String filename;
			
		  readFile(filename);		 
	  }
		
	   public void readFile(String filename){
		   this.filename = filename;
		 
		   glyphs = new ArrayList<Glyph>();
		   String n = null;  
		   BufferedReader fh;
		try{
			fh = new BufferedReader(new FileReader(filename));	
			while((n = fh.readLine()) != null && (n = n.trim()).length() > 0){

				f = n.split("\t");
				skeeing = Integer.parseInt(f[5].trim());
				bottom = Integer.parseInt(f[6].trim());
			   
				width = bottom - skeeing; 
				int fixvalue1 = 60;
				int fixvalue2 = 27;
 
			  
				String text = f[5];
				Color color = new Color(Integer.parseInt(f[7]));

				glyphs.add(new Glyph(skeeing, fixvalue1, width, fixvalue2, color, text));

			}
			//fh.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	   }
	  public void paintComponent(Graphics g) {
	  super.paintComponent(g);
	  for (Glyph glyph : glyphs){
	  glyph.draw(g);
	  }
				  
	  }
			  private void createEmptyImage()
		{
			image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
			g2d = (Graphics2D)image.getGraphics();
			g2d.setColor(Color.BLUE);
			g2d.drawString("Studying Hard", 40, 15);
		}

		public void clear()
		{
			createEmptyImage();
			repaint();
		}
	  public void mousePressed(MouseEvent e) {
							 
	  } 
	  public void mouseDragged(MouseEvent e) {
						 
	  }
	  public void mouseReleased(MouseEvent e) {
					  
	  }
	  public void mouseMoved(MouseEvent e) {
	  for(Glyph g : glyphs){
	  g.showLabel( g.contains(e.getX(), e.getY()) );
	  }
	  scaledvalue = start * 100;

	  repaint();
	  }
   
	  public void mouseClicked(MouseEvent e) {}
	  public void mouseEntered(MouseEvent e) {}
	  public void mouseExited(MouseEvent e) {}
	  }

	  static class Glyph {
	  private Rectangle bounds;
	  private Color color;
	  private Paint paint;
	  private String label;
	  private boolean showLabel = false;
	  public Glyph(int x, int y, int width, int height, Color color, String label) {
	  bounds = new Rectangle(x, y, width, height);
	  this.color = color;
	  this.paint = new GradientPaint(x, y, color, x, y+height, Color.WHITE);
	  this.label = label;
	  }
	  public void draw(Graphics g){
	  Graphics2D g2 = (Graphics2D)g;
	  g2.setPaint(paint);
	  g2.fill(bounds);
	  if (showLabel){
	  g2.setColor(Color.BLACK);
	  int labelWidth = g2.getFontMetrics().stringWidth(label);
	  int fontHeight = g2.getFontMetrics().getHeight();
	  g2.drawString( label,
	  (int)(bounds.getX()),
	  (int)(bounds.getY()));
	  }
	  }
	  public boolean contains(int x, int y){
	  return bounds.contains(x,y);
	  }
	  public void showLabel(boolean show){
	  showLabel = show;
	  }
	  }
	  public static void main(String args[]) {
		
		 String filename = args[0];
		 
		  
		 InnerClass test = new InnerClass();
		 test.readFile(filename);
		 
	  java.awt.EventQueue.invokeLater(new Runnable() {
	  public void run() {
	  new Class1().setVisible(true);
	  }
	  });
	  }
	public Color getColor(){
		return color;
	}

	public String toString() {
		return String.format("Color=%s,top=%d,bottom=%d,width=%d", color.toString(), skeeing, myfix1, width, myfix2, text);
	}
	  }


Thanks
Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Constructor parameters issue

Posted 23 June 2009 - 06:53 AM

Do you realise how difficult you are going to be making java for yourself because of the way you code? The first thing you really should look at is seperating all your logic, business and view cose into their own classes. Mixing the gui code with business code is a very bad idea. As to your problem, its not finding your specified file. The way you do it looks like it will search straight into your root project directory. So, do you have your text file in the same folder as where you have this java file? Also, are you adding the extension onto your filename anywhere? e.g. myfile.txt
Was This Post Helpful? 0
  • +
  • -

#10 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Re: Constructor parameters issue

Posted 23 June 2009 - 08:45 AM

View Postnick2price, on 23 Jun, 2009 - 05:53 AM, said:

Do you realise how difficult you are going to be making java for yourself because of the way you code? The first thing you really should look at is seperating all your logic, business and view cose into their own classes. Mixing the gui code with business code is a very bad idea. As to your problem, its not finding your specified file. The way you do it looks like it will search straight into your root project directory. So, do you have your text file in the same folder as where you have this java file? Also, are you adding the extension onto your filename anywhere? e.g. myfile.txt


Hi I also don't like to complicate things but it is the need to be like this because I am doing so many things. This code works perfectly when I write the file name inside it. The only problem is when I try to take it as command line argument. I know its basic but the way program is it seems difficult for me. That's why I need help. Yes I am taking the file from the same folder and they are all text files with .txt extension.

I would appreciate any help.

Thanks in advance

This post has been edited by crochunter: 23 June 2009 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#11 Get.carter()  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 31
  • Joined: 17-June 09

Re: Constructor parameters issue

Posted 23 June 2009 - 09:09 AM

I had a similar problem a few weeks ago. I could only readFile when I physically pointed to the file myself. The file was in the same folder but just wouldn't be read. Only difference was it was a dat.ext - not txt.
I had written a static method which was in its own class. I thought maybe there was a problem with my method but just out of curiosity I typed the full path into my method and it worked.
Not sure why this happened maybe someone can enlighten me.
Might be worth a try?

("C:\\Java\\projects\\Client Database\\Clients.dat")

regards carter.
Was This Post Helpful? 0
  • +
  • -

#12 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Constructor parameters issue

Posted 23 June 2009 - 09:30 AM

So it works ok if you hard code the location? I dont see where in your code you are asking for input from the command line, but maybe i just missed it. But what tends to happen is say your file is called test, you just type in test. A lot of times, you need to actually type in test.txt or add the extension into your readfile method. Try adding the extension onto the end of your filename.
Was This Post Helpful? 0
  • +
  • -

#13 crochunter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 29-May 09

Re: Constructor parameters issue

Posted 23 June 2009 - 09:52 AM

PBL where are you ?? Need your help urgently.

This post has been edited by crochunter: 23 June 2009 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2699
  • View blog
  • Posts: 15,736
  • Joined: 24-September 08

Re: Constructor parameters issue

Posted 23 June 2009 - 10:28 AM

I would try seperating everything into their own little methods and such. Pulling values from a parameter in a constuctor I usually put those in my mutator method. Also, your data security looks a little ... not secure. Make a deep copy constructor, everything private. You can always use a getmyfix1 method, that way an outside method can't change it.

Try as I said before. Encapsulation ensure's that you at least know what goes with what, and that you know what you're doing. After that you assemble the puzzle that is your program :)
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Constructor parameters issue

Posted 23 June 2009 - 04:44 PM

View Postcrochunter, on 23 Jun, 2009 - 08:52 AM, said:

PBL where are you ?? Need your help urgently.

Still try to figure out waht the problem is
Do we have compilation error or run time error ?
Where are these errors posted.
I do not agree with Nick very often but here he his more than just right... your code is an horrible mix of GUI and business rule stuff
it should be quoted as things not to do in OO prgramming
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2