Results 1 to 3 of 3
  1. #1
    rajuchacha007 is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

    Default passing hashmap from a method and call it within main

    Dear all,

    I hope you are doing good. I am trying write a method which takes hashMap as an argument. I would like to know while calling it in main, what kind of changes should one make? I want to pass parameters via args[] while running on ommand line. Therefore, I thought instead of e\writing hasMap.put(k,v); , I should pass it through a method and should call the method in main. I am writing here a sample code. Please let me know where i am going wrong.
    Thank you in advance.

    Best regards.

    Java Code:
    public void generateReport(HashMap hm) {
    		try {
    			Class.forName(driver).newInstance();
    			conn = DriverManager
    					.getConnection(url + dbName, userName, password);
    			System.out.println("Connected to the database");
    			System.out.println("Filling report...");
    			//hm.put("Modified_status_code", "1");
    			// used for synchronization between database and source file.
    			JasperPrint print = JasperFillManager.fillReport(fileName, hm, conn);
    			System.out.println(conn);
    			// define and initialize jasperreport engine's exporter for .pdf
    			JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
    			// parameter used for the destined file.
    			exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
    					outFileNamePDF);
    			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
    			// export to .pdf
    conn.close();
    		} catch (JRException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} catch (InstantiationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			System.exit(1);
    		} catch (IllegalAccessException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			System.exit(1);
    		}
    	}
    public static void main(String[] args) {
    		HashMap hm2=new HashMap();
    		new JasperTest().generateReport(hm2);
    		System.out.println("No. of argumetns are: " + args.length);
            for(int i= 0;i < args.length;i++)
            System.out.println("Argument " + i + " is : " + args[i]);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    You are passing an (empty) HashMap to your generateReport method alright. What's the trouble?

    kind regards,

    Jos

  3. #3
    rajuchacha007 is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

    Default

    @JosAH

    Thank you for your reply. Actually, I want to pass value at the run time. Therefore I thought I should pass HashMap in a method. Earlier I used to do it by hm.put("","");. But now I am not sure how I am going to pass the value now. Let's say if I want to pass "1" at runtime, how should I do it within main as an args[]. I would welcome your ideas and suggestions.


    Best regards.

Similar Threads

  1. Passing values to main(String args[]) ?
    By al_Marshy_1981 in forum New To Java
    Replies: 6
    Last Post: 02-21-2010, 10:16 PM
  2. passing values from main page to pop up window
    By vicky in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-29-2008, 03:06 PM
  3. Rewrite as a function so it can call from main.
    By thangli in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 06:26 AM
  4. Replies: 5
    Last Post: 10-04-2008, 11:13 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:16 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
  •