Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:45:30+00:00 2026-06-06T04:45:30+00:00

Working on the problems on Project Euler to try to learn Clojure. I’m on

  • 0

Working on the problems on Project Euler to try to learn Clojure. I’m on problem 7.

I’m getting a nullpointer and I can’t work out what on earth I’ve done wrong.

;;By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can
;;see that the 6th prime is 13.
;;
;;What is the 10 001st prime number?

(defn isPrime [x]
    (if-not (> x 2) false (loop [i 2]
            (if (= i x)
                true
                (if (= (mod x i) 0) false (recur (inc i)) )
            )
        )
    )
)


(loop [ x [2]
        y 3   ]
    (if (= (count x) 6)
        (println (last x))
        ( if (isPrime y) ((let [x (conj x y)]) (println (str (last x)))) )
    )
    (if-not (= (count x) 10001) (recur x (inc y)) )
)

The error is with the ( if (isPrime y) ((let [x (conj x y)]) (println (str (last x)))) ), according to java.

Here’s the traceback..

gcoles@gcoles-pc:~/euler$ clj 7.clj 
2
Exception in thread "main" java.lang.NullPointerException
    at user$eval2.invoke(7.clj:21)
    at clojure.lang.Compiler.eval(Compiler.java:6511)
    at clojure.lang.Compiler.load(Compiler.java:6952)
    at clojure.lang.Compiler.loadFile(Compiler.java:6912)
    at clojure.main$load_script.invoke(main.clj:283)
    at clojure.main$script_opt.invoke(main.clj:343)
    at clojure.main$main.doInvoke(main.clj:427)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.lang.Var.invoke(Var.java:415)
    at clojure.lang.AFn.applyToHelper(AFn.java:161)
    at clojure.lang.Var.applyTo(Var.java:532)
    at clojure.main.main(main.java:37)

The nullpointer actually doesn’t occur until the second time around the loop (Seen by putting a print after that let), so this confuses me a bit..

Anyone got any ideas on this?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:45:31+00:00Added an answer on June 6, 2026 at 4:45 am

    The let is of the following form:

    (let [binding values]
      (body))
    

    This will return the result of evaluating body. In the code above, your let form looks like this:

    (let [x (conj x y)])
    

    Notice how there’s no body. Because of this, the let form returns nil. Since it’s nil, the code you’re really envoking looks like this:

    (nil (println (str (last x))))
    

    Because of this, you’re effectively trying calling nil as a function. This causes your null pointer exception. If, for example, you make your code:

    (let [x (conj x y)] (println (str (last x))))
    

    It will print out the last value of x. However, it’s worth noting that this new x (the result of (conj x y)) is scoped only to the let statement. If you actually want to use it elsewhere, you’ll have to, for example, recur with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on the Project Euler problems to try and learn python and
To help me learn Haskell, I am working through the problems on Project Euler.
I'm working on problem four of Project Euler and am running into a stackoverflow
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet
everyone, I've started working yesterday on the Euler Project in Clojure and I have
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I am working on Problem 14 on Project Euler, and my code seems to
Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
I'm again working on Project Euler, this time problem #4. The point of this
I've been working through problems on Project Euler , and some of the solutions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.