newbie question

83 views
Skip to first unread message

lesni bleble

unread,
Apr 18, 2011, 7:48:28 AM4/18/11
to Clojure
hello,

i'm trying to start learning clojure with lein but i get into
trouble. I'm doing simple project:

> lein new foo
> cd foo
> lein deps
> echo '(println "hello")' >> src/foo/core.clj
> lein run -m foo.core
hello
Exception in thread "main" java.lang.NullPointerException
(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.eval(Compiler.java:5415)
at clojure.lang.Compiler.eval(Compiler.java:5391)
at clojure.core$eval.invoke(core.clj:2382)
at clojure.main$eval_opt.invoke(main.clj:235)
at clojure.main$initialize.invoke(main.clj:254)
at clojure.main$null_opt.invoke(main.clj:279)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:369)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
Caused by: java.lang.NullPointerException
at user$eval13.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:5424)
... 12 more

obviously my code ((println "hello")) was executed followed by
exception which i don't understand. What is wrong?

Ambrose Bonnaire-Sergeant

unread,
Apr 18, 2011, 9:03:17 AM4/18/11
to clo...@googlegroups.com
Hi,

lein run looks for a -main function in your source file. This is a convention
borrowed from java (and other languages).

The null pointer exception is probably lein or clojure looking for -main.

> echo '(defn -main [] (println "no exception)' >> src/foo/core.clj
> lein run -m foo.core 
hello
asdf

Ambrose

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

semperos

unread,
Apr 18, 2011, 9:04:49 AM4/18/11
to clo...@googlegroups.com
To use lein run, you need to do two things:

1) Write a -main function (defn -main [] ... ) in whatever namespace you like (you could put it in the core namescape, like you did in your echo statement for the (println "foo")). For example:

(defn -main []
  (println "foo"))

2) Specify which namespace holds this main function in your project.clj file in the root of your project. It's just another entry, like :dependencies, and it looks like this:

:main foo.core

Now when you run 'lein run', it will find your main function in that namespace and run it. If this seems a bit random, it's part Clojure's Java heritage. It's basically a shortcut for running your program as if it were an exectuble jar, which must have a "main" function that acts as an entry point for the program. The command 'lein run' allows you to treat your program as an executable jar without having to actually run 'lein uberjar' and then execute it separately.

-Daniel

Mark Nutter

unread,
Apr 19, 2011, 4:39:10 PM4/19/11
to clo...@googlegroups.com
Hmm, in your example you say you're code looks like

(println "hello")

but in your description you say

((println "hello"))

Don't know if that's a just a typo or not, but if you actually did
type in doubled parens like that it would give a null pointer
exception because println returns nil, which the extra parens would
try to execute as a function.

FWIW

lesni bleble

unread,
Apr 20, 2011, 8:55:48 AM4/20/11
to Clojure
thanks all, now it's clear to me.

On Apr 19, 10:39 pm, Mark Nutter <manutte...@gmail.com> wrote:
> Hmm, in your example you say you're code looks like
>
> (println "hello")
>
> but in your description you say
>
> ((println "hello"))
>
> Don't know if that's a just a typo or not, but if you actually did
> type in doubled parens like that it would give a null pointer
> exception because println returns nil, which the extra parens would
> try to execute as a function.

just typo. my biggest mistake in my experiments was defining main
instead -main
thanks

lesni bleble

unread,
Apr 22, 2011, 2:18:36 PM4/22/11
to Clojure
hello again,

i have another problem. I'm trying simple applet:

$ cat src/foo/applet.clj

(ns foo.applet
(:import (java.awt Graphics2D Graphics Frame Color Image Toolkit))
(:gen-class
:extends java.applet.Applet))

(defn -paint [#^Applet applet #^Graphics2D g]
(.drawString g "Hello from Clojure!" 50 50))

$ cat project.clj

(defproject foo "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:aot [foo.applet]
:warn-on-reflections true
:uberjar-name "foo-full.jar"
:dependencies [[org.clojure/clojure "1.2.1"]])

$ cat index.html

<html>
<body>
<applet
archive="foo-full.jar"
code="foo.applet.class"
width="400"
height="400">
</applet>
</body>
</html>

$ lein uberjar
$ appletviewer index.html
it works nice

$ firefox index.html
OpenJDK Runtime Environment (IcedTea6 1.8.7) (6b18-1.8.7-2)
OpenJDK Server VM (build 16.0-b13, mixed mode)
Exception in thread "Applet" java.lang.ExceptionInInitializerError
at clojure.lang.Namespace.<init>(Namespace.java:34)
at clojure.lang.Namespace.findOrCreate(Namespace.java:176)
at clojure.lang.Var.internPrivate(Var.java:94)
at foo.applet.<clinit>(Unknown Source)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:
57)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:
45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:
532)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at net.sourceforge.jnlp.Launcher.createApplet(Launcher.java:
588)
at net.sourceforge.jnlp.Launcher.getApplet(Launcher.java:548)
at net.sourceforge.jnlp.Launcher$TgThread.run(Launcher.java:
729)
Caused by: java.lang.RuntimeException:
java.security.AccessControlException: access denied
(java.lang.RuntimePermission getClassLoader)
at clojure.lang.RT.<clinit>(RT.java:305)
... 13 more
Caused by: java.security.AccessControlException: access denied
(java.lang.RuntimePermission getClassLoader)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:
393)
at
java.security.AccessController.checkPermission(AccessController.java:
553)
at
java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at
net.sourceforge.jnlp.runtime.JNLPSecurityManager.checkPermission(JNLPSecurityManager.java:
250)
at java.lang.Thread.getContextClassLoader(Thread.java:1338)
at clojure.lang.RT.baseLoader(RT.java:1560)
at clojure.lang.RT.load(RT.java:387)
at clojure.lang.RT.load(RT.java:381)
at clojure.lang.RT.doInit(RT.java:416)
at clojure.lang.RT.<clinit>(RT.java:302)
... 13 more
java.lang.NullPointerException
at net.sourceforge.jnlp.NetxPanel.runLoader(NetxPanel.java:99)
at sun.applet.AppletPanel.run(AppletPanel.java:380)
at java.lang.Thread.run(Thread.java:636)
java.lang.NullPointerException
at sun.applet.AppletPanel.run(AppletPanel.java:430)
at java.lang.Thread.run(Thread.java:636)

signing is needed? I found this example http://chouser.n01se.net/misc/tree.html
which doesn't seems to be signed but it works. I'm confused

lesni bleble

unread,
Apr 28, 2011, 5:41:59 AM4/28/11
to Clojure
Simple switch from openjdk to sun-java6 problem resolved.
> signing is needed? I found this examplehttp://chouser.n01se.net/misc/tree.html
Reply all
Reply to author
Forward
0 new messages