# Solve Code Errors

**URL:** <https://discuss.ampl.com/t/solve-code-errors/371>\
**Category:** Support\
**Tags:** errors-and-messages\
**Created:** [February 11, 2023, 6:00pm UTC](https://discuss.ampl.com/t/solve-code-errors/371 "2023-02-11T18:00:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/thiago/32/164_2.png) [@Thiago](https://discuss.ampl.com/u/Thiago)\
**Post date:** [February 11, 2023, 6:00pm UTC](https://discuss.ampl.com/t/solve-code-errors/371/1 "2023-02-11T18:00:12Z")

</div>

Hello!

I’m new to developing code in AMPL. I started studying some tutorials and I’m trying to solve some exercises available on the internet.

I tried to develop a code for the problem shown at the end of this video link: [AMPL Tutorial - YouTube](https://www.youtube.com/watch?v=6XBoPbfsk_M)

I’m getting some error messages, but I can’t figure out what’s wrong with my code.

The error that I can´t solve right now is:  
“j is not defined”  
context: maximize profit: sum{j in J} Pj[j] \* Xj[j] - sum{j in J} Kj[j] \* DELTAj[j] + \>\>\> Cj[j] \<\<\< \* Xj[j] ;

I would greatly appreciate it if anyone could help me with any other errors you may encounter. I’ve attached the .mod, .dat and .run files I wrote.

 ![mod](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/85cf07b44758c232862454578a305664f4a9889f.jpeg)

 ![dat](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/193f683b5df809fda6dd1272cd9f07f2dacbc62a.jpeg)

![run](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/751e6610e6e05199c11aa6b338aeb8a138e2fb90.jpeg)

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [February 13, 2023, 2:26am UTC](https://discuss.ampl.com/t/solve-code-errors/371/2 "2023-02-13T02:26:11Z")

</div>

It appears that you want the objective’s third term, `Cj[j] * Xj[j]`, to also be summed over `{j in J}`:

```auto
maximize profit: 
  sum{j in J} Pj[j] * Xj[j] - sum{j in J} Kj[j] * DELTAj[j] + sum{j in J} Cj[j] * Xj[j] ;

```

Since all of the sums are over `j in J`, you could also write this as

```auto
maximize profit: 
  sum{j in J} (Pj[j]*Xj[j] - Kj[j]*DELTAj[j] + Cj[j]*Xj[j]) ;

```

(In general, an index like `j` that appears in a sum operator is defined only until the next + or - sign that is outside of parentheses, or until the end of the statement.)
